Copied to clipboard

Flag this post as spam?

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


  • Mario Lopez 168 posts 952 karma points MVP 3x c-trib
    Jul 05, 2016 @ 05:04
    Mario Lopez
    0

    Submitting form with custom model

    I have tried to find an aswer to this for hours with no luck, not sure if I'm doing something wrong (probably) or it's some caveat of Umbraco.

    The thing is that I want to avoid partial views usign route hijacking. Just for clarity I don't want to have a template that the only thing it does is render a child action.

    So I have created my mvc controller like this:

    public class MembershipController : RenderMvcController    
    {
        public ActionResult MemberSignUp(RenderModel renderModel)
        {
            var model = new MemberSignupViewModel(renderModel.Content);
            return CurrentTemplate(model);
        }
    }
    

    The surface controller where I want to post the form to:

    public class MembershipFormsHandlerController : SurfaceController
    {
        [HttpPost]
        public ActionResult MemberSignUp(MemberSignupViewModel model)
        {
             //Member sign up code
        }
    }
    

    My view model:

    public class MemberSignupViewModel : RenderModel
    {
        public virtual string Password { get; set; }
    
        public string Name { get; set; }
    
        public string Email { get; set; }
    
        public MemberSignupViewModel(IPublishedContent content) : base(content)
        {
    
        }
    }
    

    And my view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MemberSignupViewModel>
    
    @using (Html.BeginUmbracoForm<MembershipFormsHandlerController>("MemberSignUp", null, new { @class = "validate edit_form" }))
    {
     <div class="block">
           @Html.LabelFor(m => Model.Details.Email)
           @Html.TextBoxFor(m => Model.Details.Email, new { autofocus = "true" })
     </div>
     <div class="block">
           @Html.LabelFor(m => Model.Password)
           @Html.TextBoxFor(m => Model.Password)
     </div>
     <button type="submit"> Save Details</button>
    }
    

    The form renders properly but when I submit it I get the error:

    Cannot bind source type Umbraco.Web.Models.RenderModel to model type TNT.TopCoach.ViewModels.MemberSignupViewModel.

    I guess the way the Umbraco routing works it is trying to send a RenderModel to my controller. Is there any way to return the view model?

    Just for testing I have tried to inherit my view from UmbracoTemplatePage. Obviously I lose my strongly typed model but I can create an empty view model to create my form.

    This works ok for this example but what about if I want to create a form to edit my members details? Is there anyway to get the populated view model without using partial views?

Please Sign in or register to post replies

Write your reply to:

Draft