FormExtensions.BeginForm Method (HtmlHelper)
Writes an opening <form> tag to the response. When the user submits the form, the request will be processed by an action method.
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
Parameters
- htmlHelper
- Type: System.Web.Mvc.HtmlHelper
The HTML helper instance that this method extends.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type HtmlHelper. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).The BeginForm method renders a form that will be handled by a controller action method.
You can use this method in a using block. In that case, the method renders the closing </form> tag at the end of the using block.
If you need to add an id to your form, you'll have to use the overload with htmlAttributes. These overloads also require that you specify the action and other params and this 'erases' the returnUrl magic that is done for you.
If you are posting back to the same action (as Login typically does), then you can manually build the <form> and set the action to the RawUrl of the HttpContext.Request (which contains the returnUrl).
Else, you can extract the returnUrl from the RawUrl.
- 9/10/2010
- LukePuplett
- 9/10/2010
- LukePuplett
If .BeginForm() was called in a View rendered by CustomerController.Signin() it will render HTML as below.
<form action="/Customer/Signin?ReturnUrl=%2fCustomer%2fBillGates%2fHome" method="post">
That is to say, it sets the action property to the same action as the one that rendered the view, but it also adds a ReturnUrl* param and takes care of setting the method to POST.
So, to handle the submission, you'd need the following in your controller:
[HttpPost]CustomerController.Signin(someModel model, string returnUrl)
And your form inputboxes and stuff will be posted back to IIS and automagically filled-in/routed by the MVC cleverness into the model instance passed in to the method, presuming that you've used the same model to build the form as you have spec'ed in the method above.
*the returnUrl is set when you browse to an action marked [Authorize] and have been bounced to your log-in method as set in the web.config.
Note to MSDN authors: please update the documentation to explain such things, it's not exactly 'documentation' if it doesn't document how it works. Adding contentbug for this reason.
- 8/4/2010
- LukePuplett
- 8/4/2010
- LukePuplett