我正在学习 .NET MVC 5,并且在使用 ModelBinder 时对方括号有疑问。
[HttpPost]
public ActionResult Submit([ModelBinder(typeof(CustomerBinder))] Customer cust)
{
return View("Customer", cust);
}
我不明白方括号如何作用于对象。 表单中的数据如何传输到 CustomerBinder? 提交操作参数的代码流程一般是怎样的。
提前致谢:)
请您参考如下方法:
这些被称为 attributes ,并且有一个 msdn turotial 解释它们 here .
Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection. For more information, see Reflection
编辑:
为了具体说明 MVC 如何使用属性,我建议继续您的 MVC 教程,因为这是一个较长的答案。如果你想要我找到的一个,here's a good one .
简而言之,
[HttpPost]属性让 MVC 知道只允许 POST 请求通过此方法,而不是其他 Http 动词,如 PUT 或 GET。[ModelBinder(typeof(CustomerBinder))]属性通知 Controller 您希望它用于传入对象的绑定(bind)器。
