Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

asp.net mvc - How does @Html.BeginForm() works?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...