Working with Asp.net C#, MVC3 Razor View, I am trying to set dropdown list value while editing a record and have following Model:
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; }
.....
}
Controller:
public ActionResult Edit(string id)
{
EditEmployeeModel model = respo.GetEmployeeByID(id);
model.Departments = GetDepartments();
return View(model);
}
And View:
<div class="editor-field">
@Html.DropDownListFor(m => m.DepartmentId, Model.Departments, new { id ="ddlstDepartments"})
@Html.ValidationMessageFor(m => m.Department)
</div>
But when view is loaded for editing, dropdownlist always shows the default/first item selected. Could someone please help here.