我有一个 MVC 应用程序。这是我的代码的一部分:
查看模型:
public class StudentViewModel
{
public int Id { get; set; }
public int? DepartmentId { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public IEnumerable<SelectListItem> AllDepartments { get; set; }
}
查看:
@Html.DropDownListFor(model => model.DepartmentId, Model.AllDepartments)
Controller :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(StudentViewModel studentViewModel)
{
用户更改下拉列表中的部门选择并提交后,由于某种原因,studentViewModel.DepartmentId 包含旧的 DepartmentId。 StudentViewModel.Name 确实包含新值。我错过了什么?
最佳答案
根据注释,您在下拉列表之前添加了属性 DepartmentId
的另一个控件。 DefaultModelBinder
按顺序读取所有表单值(加上路由值、查询字符串值等)。设置属性值后,同一属性的任何后续值都将被忽略。
关于asp.net-mvc - 无法将新选择的下拉值传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28684233/