Copied to clipboard

Flag this post as spam?

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


  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Sep 14, 2012 @ 00:25
    Peter Gregory
    0

    MVCBridge Tip - Goodbye form runat server

    Another tip for those that want to use MVC Bridge with Umbraco 4.9 or less.  I was converting a site over and due to the way the forms were formatted they needed to have css classes on the form tag.  However due to the way the CSS and the HTML was in the old code it meant that if I applied the styles to the <form runat="server"> tag that webforms apps need it would cause the layout to be wrong.

    Well it turns out with MVCBridge you dont actually need a <form runat="server"> on the page at all.  You can remove it! YAY (unless you have a usercontrol that does postback on the page).

    So that was good... However there is one thing to be aware of.  If you do a 

    @using (Html.BeginForm("ActionName","ControllerName", FormMethod.Post, new{@class="someClass"}))

    You will notice that the form tag produced will be 

    <form action"/ControllerName/ActionName" method="post" class="someClass">

    Which unfortunately is not going to work, as this will be posting the form to a route and so depending on how you are setup will mean that if you do a Return PartialView(model) in your controller it will either fail entirely or only return you the partial view html.

    No fear!  You can overcome this with a little messing around with the Html.Begin form to tell it to post to the page the form is on.

    @using (Html.BeginForm(null, null, FormMethod.Post, new{ @action = this.Node().Url, @class="someClass"}))

    Now when the tag is rendered on the page it looks like this

    <form action="/yourcurrentpages/url" method="post" class="someClass">

    And your form will post as expected with the model populated :)

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Sep 14, 2012 @ 06:47
    Robert Foster
    0

    Also note that MVC Bridge supports multiple forms on a page by providing for a unique Form Token.  However you still need to render the form token in the form which can be done with

        @Html.RenderFormToken()

    This essentially renders a hidden field, which MVC Bridge uses internally to determine which controller/action to use.

    One other thing: MVC4 Doesn't work with MVC Bridge (or Umbraco 4.9 out of the box).  Make sure you use MVC3.  Got bitten by that myself recently when building a new project in VS2012...

     

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Sep 14, 2012 @ 06:51
    Robert Foster
    0

    lol - should have taken a look at the updates to the project - turns out that MVC Bridge now uses the AntiForgeryToken instead of the Form Token now - sweet!

  • Peter Gregory 408 posts 1614 karma points MVP 3x admin c-trib
    Sep 14, 2012 @ 14:06
    Peter Gregory
    0

    By doing what I have suggested your page can have as many forms on it as you like (as long as you dont have any usercontrols that have postback, in which case you still need the asp.net form runat=server).

     

  • Robert Foster 459 posts 1820 karma points MVP 3x admin c-trib
    Sep 17, 2012 @ 13:46
    Robert Foster
    0

    Just thinking about this a little more...

    What we might need is another HtmlHelper extension in the MVCBridge library to allow us to provide the extended attributes without having to provide the Action and Controller parameters.  The Devotit.Umbraco.MvcBridge.Html.MvcBridgeExtensions class needs something like the following:

     

            public static System.Web.Mvc.Html.MvcForm BeginForm(this HtmlHelper helper, FormMethod method, object htmlAttributes) {
    return helper.BeginForm(null, null, method, htmlAttributes);
    }

    And perhaps a few more to cover the various other extensions in the FormsExtensions helper. (Is it valuable to be able to specify the routedictionary as well?)

    Just a thought.

  • Sergey 5 posts 25 karma points
    Sep 21, 2012 @ 22:36
    Sergey
    0

    Hi. Actually, everything is done for you by mvc engine. As umbraco page being hit with mvc route(you can map static mvc route to any page), RouteData object is created automatically which contains controller and action from route, so the mvc bidge code can contain something like this:

    var routedata = null;
    if (parentContext.RouteData.Route == null)
    {
    ...standard mvcbridge way to assign routedata which actually does it already but overrides controller and action values
    }else
    {
    routedata = parentContext.RouteData;
    }

     

    Cheers!

Please Sign in or register to post replies

Write your reply to:

Draft