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

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

问题描述

我很新的ASP.NET,今天刚开始的MVC教程asp.net。我来到这里的http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-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

到目前为止好,问题:

在我看来,我有以下code
(模型设置为与@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>
}

我MovieController

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

这是一个问题 - 如何赫克它的电影对象传递给上述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" ... />

在这里,我不明白为什么行动=非常相同的视图页面的网址是什么?!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 在code版本,
不带任何参数,发送一个HTTP POST到当前的URL,因此,如果观点是响应
/电影/编辑/ 5 ,开头的表单标签将如下所示:
&LT;形式的行动=/电影/编辑/ 5的方法=POST>

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辅助询问路由引擎如何实现的编辑操作
MovieController。在幕后,它使用一个名为GetVirtualPath对路线的方法
财产RouteTable暴露 - 这就是你的Web应用程序注册的所有航线
Global.asax中。如果你做了这一切,没有一个HTML帮手,你必须写以下所有
code:

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

在一个电影的情况下
对象,默认的模型绑定检查的电影,并认为所有电影可用属性
为绑定。继先前检查的命名约定,默认的模型绑定可自动转换和移动从请求值到一个电影对象(模型绑定可
还创建对象的实例来填充)。
换句话说,当模型绑定看到一个电影有一个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天全站免登陆