2010-02-10 65 views

回答

2

MVC根據名稱自動將表單值映射到Action參數。字符串和原始值類型很容易。

[HttpPost] 
public ActionResult AttemptLogin(string username, string password) 

我們也可以使用實體類型作爲操作參數。在這種情況下,將使用默認的ModelBinder,並嘗試綁定與「parameterName.PropertyName」的模式相匹配的任何後期數據。如果我的表單包含名爲「user.FirstName」的字段,則我的用戶對象將具有該屬性集。

[HttpPost] 
public ActionResult Save(User user) 

自定義ModelBinders和BindAttribute爲模型綁定提供了額外的靈活性。

// do not let MVC bind these properties 
[Bind(Exclude="Created, Modified")] 
public class User 

我可以有一個用戶自定義聯編程序,用於更改我自己的詳細信息屏幕。這可能只有名字,姓氏和電子郵件屬性。

[HttpPost] 
public ActionResult ChangeDetails(guid Id, [ModelBinder(typeof(UserChangeDetailsBinder))] User user) 

如果我有一個自定義聯編程序應該用來代替默認的聯編程序,它會在global.asax.cs中註冊。

ModelBinders.Binders[typeof(User)] = new UserBinder(); 

您還可以從Request["fieldname"]中讀取表格值。

+1

在MVC2中改爲'AcceptVerbs(「POST」)'我們使用'HttpPost'屬性。 – 2010-02-10 08:35:49

+0

非常感謝你 – mary 2010-02-10 08:59:22

相關問題