2012-11-04 80 views
0

我正在顯示用戶列表。每個用戶記錄都有一個TrainingEvent對象屬性(來自多個TrainingEvent的選擇)。我創建了一個視圖模型來包含用戶列表和TrainingEvents列表。MVC3 DropDownListFor未選擇保存的值

視圖模型:

public class RegistrationViewModel 
{ 
    public IEnumerable<Domain.Entities.User> Users { get; set; } 
    public IEnumerable<SelectListItem> TrainingEvents { get; set; } 
} 

(注意:Domain.Entities.User包含TrainingEvent對象屬性)

控制器:

public ActionResult Dealership(int id) 
{ 
    var model = new RegistrationViewModel 
    { 
     Users = repository.Find 
      .Where(u => u.Dealer.DealerId == id).OrderBy(u => u.LastName), 
       TrainingEvents = repository.TrainingEvents.ToList() 
            .Select(x => new SelectListItem 
              { 
              Text = x.Date.ToString() + " - " + x.LocationName, 
              Value = x.TrainingEventId.ToString() 
              }) 
    }; 

    return View("East", model); 
} 

結合在爭奪瓦特:

@foreach(Company.Domain.Entities.User usr in Model.Users) 
    { 
    <tr> 
     <td>@usr.LastName</td> 
     <td>@usr.FirstName</td> 
     <td>@usr.JobDescription</td> 
     <td>@Html.DropDownListFor(m => m.Users.FirstOrDefault(u => u.UserId == usr.UserId).TrainingEvent, Model.TrainingEvents)</td> 
    </tr> 
} 

在使用者列,並且每個用戶行具有與TrainingEvents填充下拉。但是,以前保存在用戶的TrainingEvent對象中的值未被選中。任何想法,爲什麼不呢?

回答

3

您錯誤地使用了DropDownListFor幫手。這個助手希望作爲第一個參數傳遞一個簡單的包含至多成員訪問權限的lambda表達式。在你的例子中,你試圖使用諸如FirstOrDefault擴展方法之類的東西來構造一些複雜的lambda表達式,這是不受支持的。從模型中獲取一些數據也不是視圖的責任。這是控制器的響應來填充適當的視圖模型視圖,並通過它的消費

因此,我建議你使用反映你的觀點要求的實景模型:

public class UserViewModel 
{ 
    public int TrainingEventId { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string JobDescription { get; set; } 
} 

public class RegistrationViewModel 
{ 
    public IList<UserViewModel> Users { get; set; } 
    public IEnumerable<SelectListItem> TrainingEvents { get; set; } 
} 

這會在你的控制器動作來填充:

public ActionResult Dealership(int id) 
{ 
    var users = repository 
     .Find 
     .Where(u => u.Dealer.DealerId == id) 
     .OrderBy(u => u.LastName) 
     .Select(u => new UserViewModel 
     { 
      TrainingEventId = u.TrainingEvent.TrainingEventId, 
      FirstName = u.FirstName, 
      LastName = u.LastName, 
      JobDescription = u.JobDescription 
     }) 
     .ToList(); 

    var trainingEvents = repository 
     .TrainingEvents 
     .ToList() 
     .Select(x => new SelectListItem 
     { 
      Text = x.Date.ToString() + " - " + x.LocationName, 
      Value = x.TrainingEventId.ToString() 
     }); 

    var model = new RegistrationViewModel 
    { 
     TrainingEventIds = TrainingEventIds, 
     TrainingEvents = trainingEvents 
    }; 

    return View("East", model); 
} 

終於視圖中的DropDownListFor助手綁定到視圖模型的相應值:

@for(var i = 0 ; i < Model.TrainingEventIds; i++) 
{ 
    <tr> 
     <td>@Html.DisplayFor(m => m.Users[i].LastName)</td> 
     <td>@Html.DisplayFor(m => m.Users[i].FirstName)</td> 
     <td>@Html.DisplayFor(m => m.Users[i].JobDescription)</td> 
     <td> 
      @Html.DropDownListFor(
       m => m.Users[i].TrainingEventId, 
       Model.TrainingEvents 
      ) 
     </td> 
    </tr> 
}