MVC viewmodel Edit 在 Httppost 之后什么都不显示
MVC viewmodel Edit shows nothing after Httppost
一段时间以来我一直在处理视图模型问题,我想解决一些问题。在我的视图模型中,我可以显示 "index" 并且可以添加新员工 "create",但是 "Edit" 不起作用。
我可以显示 "edit" 页面,进行编辑(比如更改名称)但是当我 post 返回时,所有数据都显示为 null.在 "create" 中,在我 post 插入之后,控制器会显示更改 (EmployeeViewModel) 并插入记录。它只是不显示何时执行 "edit".
这是视图模型中固有的东西还是有其他东西?
这是我的视图模型class(数据库优先):
public partial class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public Nullable<int> DepartmentId { get; set; }
public string Address { get; set; }
public virtual Department Department { get; set; }
}
public partial class Department
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Department()
{
this.Employees = new HashSet<Employee>();
}
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Employee> Employees { get; set; }
}
public class EmployeeViewModel
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public Nullable<int> DepartmentId { get; set; }
public string Address { get; set; }
public string DepartmentName { get; set; }
}
这是我的控制器:
public class TestController : Controller
{
public db dContext = new db();
public ActionResult Index()
{
List<Employee> employeelist = dContext.Employees.ToList();
EmployeeViewModel employeeVM = new EmployeeViewModel();
List<EmployeeViewModel> employeeVMList = employeelist.Select(x => new EmployeeViewModel
{
Name = x.Name,
EmployeeId = x.EmployeeId,
Address = x.Address,
DepartmentId = x.DepartmentId,
DepartmentName = x.Department.DepartmentName
}).ToList();
return View(employeeVMList);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create( EmployeeViewModel employeeVM)
{
if (ModelState.IsValid)
{
Employee e = new Employee();
e.EmployeeId = employeeVM.EmployeeId;
e.Name = employeeVM.Name;
e.DepartmentId = employeeVM.DepartmentId;
e.Address = employeeVM.Address;
dContext.Employees.Add(e);
dContext.SaveChanges();
return RedirectToAction("Index");
}
return View("Index");
}
public ActionResult Edit( EmployeeViewModel em , int? id)
{
var dbEmpVM = (from e in dContext.Employees
join d in dContext.Departments
on e.DepartmentId equals d.DepartmentId
where e.EmployeeId == id
select new EmployeeViewModel
{
EmployeeId = e.EmployeeId,
DepartmentId=e.DepartmentId,
Address=e.Address,
Name=e.Name,
DepartmentName=d.DepartmentName
}).ToList();
return View( dbEmpVM );
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EmployeeViewModel model, int id)
{
string name = Request.Form["EmployeeId"];
string naaanm = model.EmployeeId.ToString();
return RedirectToAction("Index");
}
}
这是我的编辑:
@model IEnumerable<MVCTutorial.Models.EmployeeViewModel>
@{
ViewBag.Title = "Edit";
}
<h4>(EmployeeViewModel)</h4>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
foreach (var item in Model)
{
<div class="form-group">
@Html.LabelFor( i => item.EmployeeId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.EmployeeId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.EmployeeId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(i => item.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(i => item.DepartmentId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.DepartmentId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.DepartmentId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(i => item.DepartmentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.DepartmentName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.DepartmentName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(i => item.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.Address, "", new { @class = "text-danger" })
</div>
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Edit" class="btn btn-default" />
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
EmployeeViewModel
您当前的代码(在编辑视图中)将生成如下所示的输入标记。
<input class="form-control text-box single-line"
id="item_DepartmentName" name="item.DepartmentName" type="text" value="SomeName" />
查看名称属性值。是"item.DepartmentName"
。您的 HttpPost 操作方法参数是 EmployeeViewModel
类型,但没有具有此名称的 属性!因此,模型绑定器将无法将从您的表单中使用此名称 ("item.DepartmentName"
) 发布的表单数据映射到您的方法参数的任何属性。
解决方案是更新输入字段的 name 属性值以匹配 EmployeeViewModel
的 属性 名称。理想情况下,您的代码应生成如下标记。
<input name="DepartmentName" type="text" value="Some value" />
您可以在调用 html 帮助程序时明确指定 name
属性值。例如,使用 Html.TextBoxFor
助手
@Html.TextBoxFor(i => item.DepartmentName,
new { @class = "form-control", NAME = "DepartmentName" })
现在,当您提交表单时,模型绑定器将能够将该表单字段的值映射到您的 EmployeeViewModel
对象的 DepartmentName
属性。
一段时间以来我一直在处理视图模型问题,我想解决一些问题。在我的视图模型中,我可以显示 "index" 并且可以添加新员工 "create",但是 "Edit" 不起作用。
我可以显示 "edit" 页面,进行编辑(比如更改名称)但是当我 post 返回时,所有数据都显示为 null.在 "create" 中,在我 post 插入之后,控制器会显示更改 (EmployeeViewModel) 并插入记录。它只是不显示何时执行 "edit".
这是视图模型中固有的东西还是有其他东西?
这是我的视图模型class(数据库优先):
public partial class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public Nullable<int> DepartmentId { get; set; }
public string Address { get; set; }
public virtual Department Department { get; set; }
}
public partial class Department
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Department()
{
this.Employees = new HashSet<Employee>();
}
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Employee> Employees { get; set; }
}
public class EmployeeViewModel
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public Nullable<int> DepartmentId { get; set; }
public string Address { get; set; }
public string DepartmentName { get; set; }
}
这是我的控制器:
public class TestController : Controller
{
public db dContext = new db();
public ActionResult Index()
{
List<Employee> employeelist = dContext.Employees.ToList();
EmployeeViewModel employeeVM = new EmployeeViewModel();
List<EmployeeViewModel> employeeVMList = employeelist.Select(x => new EmployeeViewModel
{
Name = x.Name,
EmployeeId = x.EmployeeId,
Address = x.Address,
DepartmentId = x.DepartmentId,
DepartmentName = x.Department.DepartmentName
}).ToList();
return View(employeeVMList);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create( EmployeeViewModel employeeVM)
{
if (ModelState.IsValid)
{
Employee e = new Employee();
e.EmployeeId = employeeVM.EmployeeId;
e.Name = employeeVM.Name;
e.DepartmentId = employeeVM.DepartmentId;
e.Address = employeeVM.Address;
dContext.Employees.Add(e);
dContext.SaveChanges();
return RedirectToAction("Index");
}
return View("Index");
}
public ActionResult Edit( EmployeeViewModel em , int? id)
{
var dbEmpVM = (from e in dContext.Employees
join d in dContext.Departments
on e.DepartmentId equals d.DepartmentId
where e.EmployeeId == id
select new EmployeeViewModel
{
EmployeeId = e.EmployeeId,
DepartmentId=e.DepartmentId,
Address=e.Address,
Name=e.Name,
DepartmentName=d.DepartmentName
}).ToList();
return View( dbEmpVM );
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EmployeeViewModel model, int id)
{
string name = Request.Form["EmployeeId"];
string naaanm = model.EmployeeId.ToString();
return RedirectToAction("Index");
}
}
这是我的编辑:
@model IEnumerable<MVCTutorial.Models.EmployeeViewModel>
@{
ViewBag.Title = "Edit";
}
<h4>(EmployeeViewModel)</h4>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
foreach (var item in Model)
{
<div class="form-group">
@Html.LabelFor( i => item.EmployeeId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.EmployeeId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.EmployeeId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(i => item.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(i => item.DepartmentId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.DepartmentId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.DepartmentId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(i => item.DepartmentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.DepartmentName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.DepartmentName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(i => item.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(i => item.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(i => item.Address, "", new { @class = "text-danger" })
</div>
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Edit" class="btn btn-default" />
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
EmployeeViewModel
您当前的代码(在编辑视图中)将生成如下所示的输入标记。
<input class="form-control text-box single-line"
id="item_DepartmentName" name="item.DepartmentName" type="text" value="SomeName" />
查看名称属性值。是"item.DepartmentName"
。您的 HttpPost 操作方法参数是 EmployeeViewModel
类型,但没有具有此名称的 属性!因此,模型绑定器将无法将从您的表单中使用此名称 ("item.DepartmentName"
) 发布的表单数据映射到您的方法参数的任何属性。
解决方案是更新输入字段的 name 属性值以匹配 EmployeeViewModel
的 属性 名称。理想情况下,您的代码应生成如下标记。
<input name="DepartmentName" type="text" value="Some value" />
您可以在调用 html 帮助程序时明确指定 name
属性值。例如,使用 Html.TextBoxFor
助手
@Html.TextBoxFor(i => item.DepartmentName,
new { @class = "form-control", NAME = "DepartmentName" })
现在,当您提交表单时,模型绑定器将能够将该表单字段的值映射到您的 EmployeeViewModel
对象的 DepartmentName
属性。