[ACCEPTED]-MVC: what code gets called when you click the "submit" button?-asp.net-mvc
It's not the button that defines what happens, but the 20 form itself. The button of type submit (one 19 per form) just triggers the form submission, which 18 is handled by the form itself.
A form has 17 an action - e.g.:
<form name="input" action="users/save" method="post">
<!-- Form content goes here -->
<input type="submit" value="Submit" />
</form>
The action is an URL and 16 what happens is that the browser collects 15 the values of all the fields in the form 14 (<input...>
) and posts them to the specified url.
In 13 ASP.NET MVC forms are usually defined using 12 the Html helpers, so that building the URL 11 for the form action is delegated to ASP.NET 10 MVC. For the above for example:
<% using(Html.BeginForm("Save", "Users")) %>
<% { %>
<!-- Form content goes here -->
<input type="submit" value="Save" />
<% } %>
Which in 9 this case will create a url /users/save
and the form 8 will post to that url. That in terms will 7 trigger the ASP.NET routing which will handle 6 the /users/save
url and break it into chunks so that 5 it knows that it has to invoke the "Save" action 4 method on the "Users" controller class. It 3 will then read all the incoming field name-value 2 pairs and try to map them to the method 1 parameter names if any.
It would call whatever public action method 5 the form action is pointing to on your controller. You 4 can then call save on the view model.
public virtual ActionResult Save(MyViewModel model) {
model.Save();
--- more code to do stuff here
}
Set 3 your form action to MyController/Save
You can also use using (Html.BeginForm...
in 2 your code to point the form to a specific 1 action method on a specific controller.
when you click submit button, request goes 10 to the HTTp Module which directs it to corresponding 9 controller action. when edit view is created 8 from template the post address of the form 7 is same as of the edit form i.e if you are 6 visiting /home/edit you can see following 5 html in form's opening tag
<form method="post" action="/home/edit">
you can have another 4 action method that only accepts post requests 3 like
[HttpPost]
public ActionResult Edit(int id, ViewModel model)
{
//put your logic here handling submitted values
}
HttpPost attribute tells that it will 2 only handle post request as opposed to get 1 requested used to render the form
it calls the Action method defined in the 3 action part of the form element eg:
<form action="/Account/LogOn" id="loginForm" method="post">
The LogOn 2 action in the Account controller will be 1 invoked in this form
The ViewPage has a BeginForm Method using (Html.BeginForm()
at 5 the top which would render the FormTag. This 4 method has a overload which takes ActionName 3 and controller Name. So you can specify 2 the action in your controller which has 1 to be called.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.