Copied to clipboard

Flag this post as spam?

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


  • Zakhar 171 posts 397 karma points
    Jan 23, 2014 @ 13:53
    Zakhar
    0

    Model problem with MVC form in Child action

    Hi Guys, I'm trying to follow this tutorial http://our.umbraco.org/documentation/reference/Mvc/forms/turorial-child-action but keep getting YSOD when I call return CurrentUmbracoPage(); :

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'Umbraco6Mvc.Models.MemberRegisterModel'.

    I use Umbraco 6.1.6 and my code is as follows:

    Child action call in template:

    @Html.Action("RegisterForm", "Member")
    

    Model:

    public class MemberRegisterModel
        {
            [Required, Display(Name = "Enter your name")]
            public string Name { get; set; }
    
            [Required, Display(Name = "Enter your email")]
            [RegularExpression(SiteHelpers.EmailRegExp, ErrorMessage = "Invalid email.")]
            public string Email { get; set; }
    
            [Required, Display(Name = "Password"), DataType(DataType.Password)]
            public string Password { get; set; }
        }
    

    Controller:

    public class MemberController : SurfaceController
    {
        [ChildActionOnly]
        public ActionResult RegisterForm()
        {
            return PartialView("register", new MemberRegisterModel());
        }
    
        [HttpPost]
        public ActionResult Register(MemberRegisterModel model)
        {
    
            if (!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }
        } 
    }
    

    View:

    @using Umbraco.Web
    @using Umbraco.Web.Macros
    @using Umbraco.Web.Mvc
    @using Umbraco6Mvc.Controllers
    @using Umbraco6Mvc.Models
    
    @model MemberRegisterModel
    
        @using(Html.BeginUmbracoForm<MemberController>("Register"))
        {
            @Html.LabelFor(model => model.Name)
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
    
            @Html.LabelFor(model => model.Email)
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
    
            @Html.LabelFor(model => model.Password)
            @Html.PasswordFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
    
            <input type="submit" value="Submit" />
        }
    

    It states in the documentation that to avoid the problem I should use different names for action or attribute post action with [NotChildAction] but neither of those work for me. What am I missing and how can I avoid that?

    Thanks

  • Zakhar 171 posts 397 karma points
    Jan 24, 2014 @ 16:12
    Zakhar
    100

    Ok, I found the problem, my partial view name was Register (same as HttpPost method name) and apparently it conflicted somehow.

    I renamed the view to "RegisterForm" and changed my get action method accordingly:

    [ChildActionOnly]
    public ActionResult LoginForm()
    {
       return PartialView("LoginForm", new MemberLoginModel());
    }
    

    Just posting it here in case someone has the same problem.

    Zakhar.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies