html.beginform mvc4,asp.net mvc - How does @Html.BeginForm() works? - Stack Overflow

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

}

...

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值