Ive built a form that I need to post back on. I have put…
@using (Html.BeginForm(){
}
But I don’t see the point in using the parameter-less method call because I don’t see how the form knows which URL to post to? Ive seen some example using it and they seem to generate the correct url but I dont really know why my one doesn’t?
It posts to the url that the client browser is currently pointing to. Here is how it is implemented:
As you can see it simply uses the same url as the one that was used to render the form.
The point is that in a RESTful ASP.NET MVC application you usually have 2 actions with the same name but accessible accessible through different verbs:
The first action is used to render the form and the second action is used to process the submission of the form. So when you use
Html.BeginFormwithout any arguments on the view that the first action rendered you are no longer hardcoding any action or controllers in the view. You are relying on standard ASP.NET MVC conventions. It is the right way to do.Of course if you want to post to some different action or controller you will have to use the proper overload of the BeginForm method and specify them.