2012-02-22 95 views
0

這可能已經被問過了上千次了,但是我只是經過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(字符串)。

我完全失去了現在該做什麼的軌道,有人可以幫我解決這個問題嗎?

在此先感謝!

+0

有什麼問題?什麼不適合你? – Omu 2012-02-22 19:00:37

回答

2
@Html.DropDownListFor(
    x => x.Division, 
    new SelectList(
     Model.Divisions.ToList().Select(
      x => new { Value = x.Value.DivisionId, Text = x.Value.Name } 
     ), 
     "Value", 
     "Text" 
    ) 
) 

,或者你可以將屬性添加到您的視圖模型:

public IEnumerable<SelectListItem> DivisionsList 
{ 
    get 
    { 
     return Divisions.ToList().Select(
      x => new SelectListItem 
      { 
       Value = x.Value.DivisionId.ToString(), 
       Text = x.Value.Name 
      } 
     ); 
    } 
} 

然後:

@Html.DropDownListFor(x => x.Division, Model.DivisionsList) 

或另一個屬性是不使用字典,在您的視圖型號,但直接:

[Display(Name = "Divisions")] 
public IEnumerable<SelectListItem> Divisions 
{ 
    get 
    { 
     var db = new IBPVEntities(); 
     Division = db.Division.FirstOrDefault(); 
     return db.Division.ToList().Select(x => new SelectListItem 
     { 
      Value = x.DivisionId.ToString(), 
      Text = x.Name 
     }); 
    } 
} 

並在您的意見:

@Html.DropDownListFor(x => x.Division, Model.Divisions) 
+0

謝謝!那幫了我一大筆錢,雖然......當我看到這個帖子的時候,這個部門似乎沒有設置好? – MrZunz 2012-02-22 13:27:07

3

要「解決」司,因爲你指認是很簡單的。在你的模型(CreateTeacherModel),你不會真正使用科作爲屬性類型,而是使用GUID:

public Guid DivisionId { get; set; } 

,並在你的控制器,做一個查詢以獲得實際師:

db.Divisions.First(x => x.Id == Model.DivisionId); 

如果您需要將CreateTeacherModel轉換爲實體框架對象,您可以查看Automapper爲您完成轉換(Stack Overflow在後臺執行此操作)。

如果您覺得自己喜歡,可以註冊一個自定義模型活頁夾。例如參見here