@Html.BeginForm() 如何工作? [英] How does @Html.BeginForm() works?

查看:29
本文介绍了@Html.BeginForm() 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 ASP.NET 非常陌生,今天刚开始在 asp.net 上学习 MVC 教程.我到了这里 http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examing-the-edit-methods-and-edit-view

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:

在我的视图中,我有以下代码(模型设置为@model MyFirstMVC4.Models.Movie 的视图)

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>
}

我的电影控制器

    // 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);
    }

这里有一个问题——它到底是如何将 Movie 对象传递给上面的 POST 方法的?!当我观察客户端时有

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" ... />

这里我不明白为什么 action = url 是同一个视图页面?!1同样在服务器端只有 Html.BeginForm() :(它是如何实现向什么动作方法发布以及要传递什么路由参数的?它有效,我只是不知道为什么

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

推荐答案

代码中BeginForm的版本,不带参数,向当前 URL 发送一个 HTTP POST,所以如果视图是对/Movie/Edit/5,打开表单标签如下所示:<代码>

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">

BeginForm HTML 帮助程序询问路由引擎如何到达电影控制器.在幕后,它在路由上使用名为 GetVirtualPath 的方法RouteTable 公开的属性——这是您的 Web 应用程序在其中注册所有路由的地方global.asax.如果您在没有 HTML 帮助程序的情况下完成所有这些,则必须编写以下所有内容代码:

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>

你问过电影对象是如何传递的.这称为模型绑定.当您有一个带有参数的操作时,MVC 运行时使用模型绑定器来构建范围.您可以在 MVC 运行时中为不同类型注册多个模型绑定器模型,但默认情况下的主力将是 DefaultModelBinder.

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.

如果是电影对象,默认模型绑定器检查电影并找到所有可用的电影属性用于绑定.遵循您之前检查过的命名约定,默认模型绑定器可以自动将请求中的值转换并移动到影片对象中(模型绑定器可以还创建要填充的对象的实例).换句话说,当模型绑定器看到一个 Movie 有一个 Title 属性时,它会寻找一个值在请求中命名为Title".请注意,模型绑定器看起来在请求中"而不是在表单中"收藏."模型绑定器使用称为值提供者的组件来搜索请求的不同领域.模型绑定器可以查看路由数据、查询字符串和表单集合,您可以根据需要添加自定义值提供程序.

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.

这篇关于@Html.BeginForm() 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆