16

MVC newbie question; I'm learning by playing around rather than Reading The Manual... :)

I see when I create an "Edit" view that the auto-generated view includes a "submit" button:

<input type="submit" value="Save" />

But what code gets called behind the scenes to do this save? Specifically, the model underlying this view has its own fancy save logic in code that I would want to call. How do I get the view to invoke my code instead of whatever standard code is being called invisibly behind the scenes?

Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
  • 2
    Reading the manual will be waaaay quicker :) The nerddinner tutorial is great to get yourself going with mvc. – raklos Apr 11 '11 at 13:39

5 Answers5

15

It's not the button that defines what happens, but the form itself. The button of type submit (one per form) just triggers the form submission, which is handled by the form itself.

A form has 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 what happens is that the browser collects the values of all the fields in the form (<input...>) and posts them to the specified url.

In ASP.NET MVC forms are usually defined using the Html helpers, so that building the URL for the form action is delegated to ASP.NET MVC. For the above for example:

<% using(Html.BeginForm("Save", "Users")) %>
<% { %>
    <!-- Form content goes here -->
    <input type="submit" value="Save" />
<% } %>

Which in this case will create a url /users/save and the form will post to that url. That in terms will trigger the ASP.NET routing which will handle the /users/save url and break it into chunks so that it knows that it has to invoke the "Save" action method on the "Users" controller class. It will then read all the incoming field name-value pairs and try to map them to the method parameter names if any.

Ivan Zlatev
  • 13,016
  • 9
  • 41
  • 50
7

It would call whatever public action method the form action is pointing to on your controller. You can then call save on the view model.

    public virtual ActionResult Save(MyViewModel model) {
        model.Save();            

        --- more code to do stuff here
    }

Set your form action to MyController/Save

You can also use using (Html.BeginForm... in your code to point the form to a specific action method on a specific controller.

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
5

when you click submit button, request goes to the HTTp Module which directs it to corresponding controller action. when edit view is created from template the post address of the form is same as of the edit form i.e if you are visiting /home/edit you can see following html in form's opening tag

<form method="post" action="/home/edit">

you can have another action method that only accepts post requests like

[HttpPost]
public ActionResult Edit(int id, ViewModel model)
{
      //put your logic here handling submitted values
}

HttpPost attribute tells that it will only handle post request as opposed to get requested used to render the form

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
3

it calls the Action method defined in the action part of the form element eg:

<form action="/Account/LogOn" id="loginForm" method="post"> 

The LogOn action in the Account controller will be invoked in this form

raklos
  • 28,027
  • 60
  • 183
  • 301
0

The ViewPage has a BeginForm Method using (Html.BeginForm() at the top which would render the FormTag. This method has a overload which takes ActionName and controller Name. So you can specify the action in your controller which has to be called.

Novice
  • 2,447
  • 3
  • 27
  • 33
  • 2
    Not true, it calls the post action of the same method you're currently viewing. – Jimmy Apr 11 '11 at 13:04
  • @Jimmy: Thanks for correcting. Incase there is no post action specified for the the method which invoked the view, what action will be called? – Novice Apr 11 '11 at 13:07
  • If you're on the form for action `login` by default it will post to the same action so it will post to action `login`, I believe it does this based on your current route when creating the form – Jimmy Apr 11 '11 at 13:10
  • Thanks... I tried and the same method is called and not the Index(). Thank u. – Novice Apr 11 '11 at 13:11