我有一个使用实体框架作为模型层的MVC3应用程序。从视图到控制器的MVC3剃刀传递模型
在EmployeeController,我有:
public ActionResult GetEmployeeEdit(String id)
{
// Get the desired Employee
var model = GetEmployees().FirstOrDefault(o=>o.EFolderid == id);
return View("EmployeeEdit", model);
}
private IQueryable<Employee> GetEmployees()
{
// Returns IQueryable<Employee>
return _employeeService.GetTable();
}
在EmployeeEdit我:
@model Metastorm.Domain.Models.Employee
@{
ViewBag.Title = "Employee Edit";
}
@using (Html.BeginForm("SaveEmployee", "Employee", FormMethod.Get, Model))
{
<fieldset>
<legend>Edit Employee</legend>
<br />
@Html.Label("firstName", "First Name: ")
@Html.EditorFor(o => @Model.NameFirst)
<br />
@Html.Label("lastName", "Last Name: ")
@Html.EditorFor(o => @Model.NameLast)
</fieldset>
<br />
<input class="button" id="submit" type="submit" value = "Save Employee" />
}
现在回到EmployeeController:
[HttpGet]
public ActionResult SaveEmployee(Employee employee)
{
if (ModelState.IsValid)
{
// Get the Employee Model again from Entity, Update and save
// Unfortunately, the employee object's FolderId value is null
}
// Just getting a model to satisfy the function
var model = GetEmployees().FirstOrDefault();
return View("EmployeeEdit", model);
}
时遇到的问题员工对象上的所有属性均为空,但Employee.NameFirst和Employee.NameLas除外t,这恰好是View with Html.EditorFor中暴露的属性。
总之,我得到一个Employee模型对象,它充分水合。我将这个模型从控制器传递到视图。在视图中,选定的字段被允许更新。 Employee模型然后被传回到持续更新的Controller中。
我的问题是如何保持最初从Controller传递给View的Employee模型保持不变。换句话说,我想拥有模型