我有以下場景。如何使用DefaultModelBinder綁定模型屬性 - ASP.NET MVC2
- 我已經填充了從實體框架實體(員工)
- 我從編輯/員工以保存/員工控制器動作後的模型編輯/僱員視圖。保存/僱員動作期望另一種類型的(EmployeeSave),其具有僱員作爲屬性
這是編輯/僱員方法
public ActionResult Edit(EmployeesEdit command)
{
var employee = command.Execute();
if (employee != null)
{
return View(employee);
}
return View("Index");
}
這是保存/僱員方法
public ActionResult Save(EmployeesSave command)
{
var result = command.Execute();
if (result)
{
return View(command.Employee);
}
return View("Error");
}
這是EmployeeSave類
public class EmployeesSave
{
public bool Execute()
{
// ... save the employee
return true;
}
//I want this prop populated by my model binder
public Employee Employee { get; set; }
}
MVC DefaultModelBinder能夠解析Employee和EmployeeSave類。
什麼是創建命令對象在你的例子中傳入行動方法? –
@BlessYahu ASP.NET MVC默認模型綁定器 – abx78