现在的位置: 首页 > 综合 > 正文

Asp.net MVC Model Binding

2014年02月27日 ⁄ 综合 ⁄ 共 528字 ⁄ 字号 评论关闭

1.使用MVC默认Model Binder

[AcceptVerbs("POST")]
public ActionResult Edit(Product product)

2.使用FormCollection然后自己然后手工绑定
[AcceptVerbs("POST")]
public ActionResult Edit(FormCollection form)

 3.使用UpdateModel只绑定指定的属性

UpdateModel(product, new[] { "ProductName", "UnitPrice", "Discontinued", "ReorderLevel" });

4.自定绑定
ModelBinders.Binders[typeof(Product)] = new ProductBinder(); 
或者 [AcceptVerbs("POST")]
public ActionResult Edit([ModelBinder(typeof(ProductBinder))] Product product)

5.只去指定的属性
[AcceptVerbs("POST")]
public ActionResult Edit(string ProductName, string UnitPrice)

 

抱歉!评论已关闭.