什么是 ASP.NET MVC 中的模型绑定? [英] What is model binding in ASP.NET MVC?
问题描述
ASP.NET MVC 中的模型绑定是什么,为什么需要它?谁能举个简单的例子,可以通过检查创建强类型视图来实现模型绑定吗?
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?
推荐答案
ModelBinding
是 ASP.NET MVC 用于从输入创建强类型对象(或填充原始类型参数)的机制流(通常是 HTTP 请求).
ModelBinding
is the mechanism ASP.NET MVC uses to create strongly-typed objects (or fill primitive-type parameters) from the input stream (usually an HTTP request).
例如,考虑这个 Person
模型:
For example, consider this Person
model:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
现在,您在某些 Controller
中有一些 Action
需要 Person
类型作为参数:
Now, you have some Action
in some Controller
that's expecting a Person
type as a parameter:
public class HomeController : Controller
{
public ActionResult EditPersonDetails(Person person)
{
// ...
}
}
Model-Binder
然后负责为您填充 person
参数.默认情况下,它通过查询 ValueProviders
集合并询问(待绑定)模型中每个属性的值来实现.
The Model-Binder
is then responsible to fill that person
parameter for you. By default it does it by consulting the ValueProviders
collection and asking for the value of each property in the (to be bound) model.
这篇关于什么是 ASP.NET MVC 中的模型绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!