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
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.
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...
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!
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).
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:
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?)
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;
}
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
You will notice that the form tag produced will be
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.
Now when the tag is rendered on the page it looks like this
And your form will post as expected with the model populated :)
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
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...
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!
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).
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:
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.
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:
Cheers!
is working on a reply...