這可能已經被問過了上千次了,但是我只是經過10次嘗試之後纔想起這件事。DropDownListFor來自實體框架
我試圖創建一個 '老師',其中一個可以選擇司老師是
型號:
public class CreateTeacherModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Display(Name = "Division")]
public Division Division { get; set; }
[Display(Name = "Divisions")]
public Dictionary<Guid, Division> Divisions
{
get
{
var db = new IBPVEntities();
Division = db.Division.FirstOrDefault();
return db.Division.ToDictionary(division => division.DivisionId);
}
}
控制器:
[Authorize(Roles = "Administrator")]
public ActionResult CreateTeacherTest()
{
return View(new CreateTeacherModel());
}
[Authorize(Roles = "Administrator")]
[HttpPost]
public ActionResult CreateTeacherTest(CreateTeacherModel model)
{
if (ModelState.IsValid)
{
var selectedDivision = model.Division;
// Save to database.
}
return View(model);
}
查看:
@Html.DropDownListFor(x => x.Division, new SelectList(Model.Divisions, "DivisionId", "Name"))
部門有2個屬性:DivisionId(guid)和Name(字符串)。
我完全失去了現在該做什麼的軌道,有人可以幫我解決這個問題嗎?
在此先感謝!
有什麼問題?什麼不適合你? – Omu 2012-02-22 19:00:37