Copied to clipboard

Flag this post as spam?

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


  • TempleClause 32 posts 248 karma points
    Feb 23, 2015 @ 19:40
    TempleClause
    1

    place a form inside a PartialViewMacroPage

    I have a Razor file called Reservation.cshtml. Which is a Macro Partial View.

    I would like to have a form inside it. At the start I have

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    

    I tried to add the model as well but then I always get an error message

    The 'inherits' keyword is not allowed when a 'model' keyword is used.
    

    I'm a getting a complete headache why this is so frustrating and complicated. This is what I have so far:

    @{
        var reservation = new NgakNgakBeach.Models.Reservation();
    }
    @using (Html.BeginUmbracoForm("MakeReservation", "Reservation",
     { enctype = "multipart/form-data" }, htmlAttributes, FormMethod.Post))
        {
            @Html.EditorFor(x => reservation.RoomName)
                <input type="submit" />
        }
    

    and my Surface Controller:

       [HttpPost]
        public ActionResult MakeReservation(Reservation model)
        {
            return CurrentUmbracoPage();
        }
    

    When I debug this the MakeReservation Action gets called perfectly well but the model is always empty.. Why? What is the correct way of having a form inside a PartialViewMacro with custom markup?

    I think I need to set the Model somehow, but as mentioned before I also need the @inherits Umbraco.Web.Macros.PartialViewMacroPage

    I hope you guys can help me out!

    Thanks

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Feb 23, 2015 @ 21:28
    Kevin Jump
    0

    Hi 

    there are two ways to get the model back to the controller, sometimes it depends on what else you are doing to the page, as to which one you might choose, but in summary:

    I'm not sure if you can do it with Partial view macros, but with straight partial views you can inherit partial view page and pass in your model as the type

    @inherits Umbraco.Web.Mvc.UmbracoViewPage 
    then Model. will be your model, but you still have access to all the Umbraco helpers.
    if you can't do this (because it's a macro, or another reason) you can tell your controller that the model name is diffrent in the bindings. 
    so on your Controller, add [Bind(Prefix="")] to the model argument - matching the name of the variable you assigned your class to. 
    public ActionResult MakeReservation([Bind(Prefix="reservation")]Reservation model)

     

    if you do that then your model, should be populated. inside the class. and thins like ModelState.IsValid will pass. 

     

  • Andy Shokry 5 posts 75 karma points
    Dec 12, 2018 @ 08:43
    Andy Shokry
    0
    Bind(Prefix="")]
    

    This fixed the problem. I was also able to fix the issue by another workaround.

    In your Macro view, render a partial view that has the form. Your solution is way more elegant though, rather than having two partial views serving the same purpose.

Please Sign in or register to post replies

Write your reply to:

Draft