asp.net-mvc 什么是 ASP.NET MVC 中的模型绑定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17721542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is model binding in ASP.NET MVC?
提问by rikky
What is model binding in ASP.NET MVC, Why is it needed? Can someone give simple example , Can model binding be achieved by checking create strongly typed view?
什么是 ASP.NET MVC 中的模型绑定,为什么需要它?有人可以举一个简单的例子,可以通过检查创建强类型视图来实现模型绑定吗?
回答by haim770
ModelBindingis the mechanism ASP.NET MVC uses to create strongly-typed objects (or fill primitive-type parameters) from the input stream (usually an HTTP request).
ModelBinding是 ASP.NET MVC 用于从输入流(通常是 HTTP 请求)创建强类型对象(或填充原始类型参数)的机制。
For example, consider this Personmodel:
例如,考虑这个Person模型:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Now, you have some Actionin some Controllerthat's expecting a Persontype as a parameter:
现在,你有一些Action在某些Controller该公司预计,一个Person类型作为参数:
public class HomeController : Controller
{
public ActionResult EditPersonDetails(Person person)
{
// ...
}
}
The Model-Binderis then responsible to fill that personparameter for you. By default it does it by consulting the ValueProviderscollection and asking for the value of each property in the (to be bound) model.
该Model-Binder将负责填补person参数为您服务。默认情况下,它通过查询ValueProviders集合并询问(待绑定)模型中每个属性的值来实现。
More on Value-Providers and Model-Binders on http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx/
有关价值提供者和模型绑定器的更多信息,请访问 http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx/

