Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Henrik Vincent 122 posts 616 karma points
    Feb 27, 2017 @ 17:47
    Henrik Vincent
    0

    Custom member properties not defined in controller

    Hi guys

    I'm watching the Umbraco.tv Member section, and I've run into some trouble.

    I've added a few extra custom member properties, as done in the tutorial, but my model simply don't recognize the custom values.

    My code is as follow:

    The view (MemberRegister.cshtml):

    @using BootstrappedDNDCC.Controllers
    @model BootstrappedDNDCC.Models.RegisterModel
    
    @using (Html.BeginUmbracoForm<RegisterMemberSurfaceController>    ("Register"))
    {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    
    <fieldset>
        <div class="form-group">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @placeholder = "Name" } })
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="form-group">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @placeholder = "Email" } })
            @Html.ValidationMessageFor(model => model.Email)
        </div>
        <div class="form-group">
            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control", @placeholder = "Password", type = "password" } })
            @Html.ValidationMessageFor(model => model.Password)
        </div>
        <div class="form-group">
            @Html.EditorFor(model => model.Biography, new { htmlAttributes = new { @class = "form-control", @placeholder = "Biography" } })
            @Html.ValidationMessageFor(model => model.Biography)
        </div>
        <div class="form-group">
            <input type="file" name="Avatar" class="form-control" />
            @Html.ValidationMessageFor(model => model.Avatar)
        </div>
        <button class="btn btn-lg btn-block">Register</button>
    </fieldset>
    } 
    

    The model (RegisterMemberViewModel.cs):

    using System.ComponentModel.DataAnnotations;
    using System.Web;
    
    namespace BootstrappedDNDCC.Models
    {
    public class RegisterModel
    {
    
        [Required]
        public string Name { get; set; }
    
        [Required]
        [EmailAddress]
        public string Email { get; set; }
    
        [Required]
        public string Password { get; set; }
    
        public string Biography { get; set; }
    
        public HttpPostedFileBase Avatar { get; set; }
    }
    } 
    

    And the controller (RegisterMemberSurfaceController.cs):

    using System.Web.Mvc;
    using Umbraco.Web.Models;
    using Umbraco.Web.Mvc;
    
    namespace BootstrappedDNDCC.Controllers
    {
        public class RegisterMemberSurfaceController : SurfaceController
        {
            // GET: RegisterMemberSurface
            public ActionResult Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
                return CurrentUmbracoPage();
    
            var memberService = Services.MemberService;
    
            if (memberService.GetByEmail(model.Email) != null)
            {
                ModelState.AddModelError("", "A member with that email already exists");
                return CurrentUmbracoPage();
            }
    
            var member = memberService.CreateMember(model.Email, model.Email, model.Name, "registeredMember");
    
            member.SetValue("userProfileBio", model.Biography);
            member.SetValue("userProfileAvatar", model.Avatar);
    
            memberService.SavePassword(member, model.Password);
            Members.Login(model.Email, model.Password);
    
            memberService.Save(member);
    
            return Redirect("/");
        }
    }
    }
    

    The Email, Password and Name values are recognized by the controller, but the Biography and the Avatar properties arent.

    I'm getting the following error:

    Error CS1061 'RegisterModel' does not contain a definition for 'Biography' and no extension method 'Biography' accepting a first argument of type 'RegisterModel' could be found (are you missing a using directive or an assembly reference?) BootstrappedDNDCC E:\Projects\Sites\BootstrappedDNDCC\BootstrappedDNDCC\Controllers\RegisterMemberSurfaceController.cs 25 Active

    I followed the tutorials strictly, since I'm all green to working with the APIs and controller/models for that matter.

    In my view, the values are recognized, so the problem lies only within the controller as far as I can tell.

    Am I supposed to do something special when I start up VS? I tried rebuilding the solution and project, but still no connection, so to say.

    Hope you guys can help me in the right direction, cus I feel a bit lost at the moment :)

    Thank you in advance

    Best
    Henrik

  • Steven Harland 78 posts 518 karma points c-trib
    Feb 27, 2017 @ 18:07
    Steven Harland
    101

    Hi Henrik,

    Umbraco defines it's own RegisterModel in Umbraco.Web.Models. Your code is using this model instead of your custom one. Update your Register method signature as follows and it should work:

    public ActionResult Register(BootstrappedDNDCC.Models.RegisterModel model)
    

    Steven

  • Henrik Vincent 122 posts 616 karma points
    Feb 27, 2017 @ 18:22
    Henrik Vincent
    0

    James

    You are my savior!

    Stared into the code for so long and I simply couldn't figure out where I went wrong, since some of the props were defined.

    Thank you very much for the quick reply.

    Have a great evening :)

    Best
    Henrik

Please Sign in or register to post replies

Write your reply to:

Draft