我有一个有效的 MVC 4 编辑视图,可以编辑存储的数据库记录。这是控制器的相关部分:
//
// GET: /Occurrence/Edit/5
public ActionResult Edit(int id = 0)
{
Occurrence occurrence = db.Occurrences.Find(id);
if (occurrence == null)
{
return HttpNotFound();
}
List<SelectListItem> points = new List<SelectListItem>()
{
new SelectListItem() { Text = "0 - (No Points)", Value = "0"},
new SelectListItem() { Text = "1 - (5 to 14 minutes)", Value = "1"},
new SelectListItem() { Text = "2 - (15 minutes to 1 hour)", Value = "2"},
new SelectListItem() { Text = "3 - (1 to 3 hours)", Value = "3"},
new SelectListItem() { Text = "5 - (Over 3 hours)", Value = "5"},
};
ViewBag.Points = points;
ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "EmployeeName", occurrence.EmployeeID);
return View(occurrence);
}
这是模型的点部分:
[Required(ErrorMessage = "The Points field is required.")]
public int Points { get; set; }
这是编辑视图的相关部分:
<div class="editor-field">
@Html.DropDownList("Points")
@Html.ValidationMessageFor(model => model.Points)
</div>
当我尝试编辑记录时,无论数据库中存储什么值,点下拉列表始终填充“0 -(无点)”。有没有办法绑定这个下拉列表,以便在编辑视图中显示存储的值?