Asp.net C#、MVC3 Razor Viewを使用して、レコードの編集中にドロップダウンリストの値を設定しようとしていますが、次のモデルがあります。
public class EditEmployeeModel
{
....
public int? DepartmentId { get; set; }
[Required(ErrorMessage = "Department is must")]
[Display(Name = "Department")]
public string Department { get; set; }
public List<SelectListItem> Departments { get; set; }
.....
}
コントローラ:
public ActionResult Edit(string id)
{
EditEmployeeModel model = respo.GetEmployeeByID(id);
model.Departments = GetDepartments();
return View(model);
}
そして見る:
<div class="editor-field">
@Html.DropDownListFor(m => m.DepartmentId, Model.Departments, new { id ="ddlstDepartments"})
@Html.ValidationMessageFor(m => m.Department)
</div>
ただし、ビューが編集のためにロードされると、ドロップダウンリストには常にデフォルト/最初に選択されたアイテムが表示されます。誰かがここで助けてくれませんか。