Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does @Html.BeginForm() works?

Tags:

I'm very new to ASP.NET, just started the MVC tutorial today on asp.net. I got here http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

So far so good, the problem:

In my View I have the following code (Model is set to the view with @model MyFirstMVC4.Models.Movie)

@using (Html.BeginForm()) {     @Html.ValidationSummary(true)      <fieldset>         <legend>Movie</legend>          @Html.HiddenFor(model => model.ID)          //... bla bla html input         <p>              <input type="submit" value="Save" />         </p>     </fieldset> } 

My MovieController

    // Shows the view     public ActionResult Edit(int id = 0)     {         Movie movie = db.Movies.Find(id);         if (movie == null)         {             return HttpNotFound();         }         return View(movie);     }      //     // POST: /Movie/Edit/5      [HttpPost] // Handles the view above     public ActionResult Edit(Movie movie)     {         if (ModelState.IsValid)         {             db.Entry(movie).State = EntityState.Modified;             db.SaveChanges();             return RedirectToAction("Index");         }         return View(movie);     } 

And here is the question - How the heck does it pass the Movie object to the POST method above?! When I observe the client side there is

<form action = "/Movie/Edit/1" ... /> 

Here I don't understand why action = url of the very same view page?!1 Also on the server side there is just Html.BeginForm() :( How does it realize to what action method to post and what route parameters to pass? It works, I just don't know why

like image 871
Ivan Prodanov Avatar asked May 24 '13 21:05

Ivan Prodanov


People also ask

Why we use HTML BeginForm?

Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html.

What is difference between HTML BeginForm and ajax BeginForm?

BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.

How do you pass a route value in HTML BeginForm?

The value will only be added as query string values if its FormMethod. Get (and you can remove the route values from the BeginForm() method). But what is the point of this - they are hidden inputs, not editable values.


2 Answers

The version of BeginForm in the code, with no parameters, sends an HTTP POST to the current URL, so if the view is a response to /Movie/Edit/5, the opening form tag will look like the following: < form action="/Movie/Edit/5" method="post">

The BeginForm HTML helper asks the routing engine how to reach the Edit action of the MovieController. Behind the scenes it uses the method named GetVirtualPath on the Routes property exposed by RouteTable — that’s where your web application registered all its routes in global.asax. If you did all this without an HTML helper, you’d have to write all the following code:

  @{  var context = this.ViewContext.RequestContext;   var values = new RouteValueDictionary{   { "controller", "movie" }, { "action", "edit" }  };   var path = RouteTable.Routes.GetVirtualPath(context, values);  }  <form action="@path.VirtualPath" method="get">   ...  </form> 

You asked how is movie object is passed. That is called model binding. When you have an action with a parameter, the MVC runtime uses a model binder to build the parameter. You can have multiple model binders registered in the MVC runtime for different types of models, but the workhorse by default will be the DefaultModelBinder.

In the case of an Movie object, the default model binder inspects the Movie and finds all the movie properties available for binding. Following the naming convention you examined earlier, the default model binder can automatically convert and move values from the request into an movie object (the model binder can also create an instance of the object to populate). In other words, when the model binder sees an Movie has a Title property, it looks for a value named “Title” in the request. Notice the model binder looks “in the request” and not “in the form collection.” The model binder uses components known as value providers to search for values in different areas of a request. The model binder can look at route data, the query string, and the form collection, and you can add custom value providers if you so desire.

like image 157
Vlado Pandžić Avatar answered Oct 07 '22 17:10

Vlado Pandžić


When you call BeginForm() without any parameters it default to using the same controller/action used to render the current page. It assumes you'll have an action with the correct name on your controller that will accept postbacks (which you do). It uses the RouteValues to do this.

It automatically binds each input control (by name) to the parameters of the action accepting the postback - or in your case, the properties of the object parameter for the action accepting the postback.

like image 25
Nick Albrecht Avatar answered Oct 07 '22 19:10

Nick Albrecht