2013-03-14 55 views
0

我有一個名爲FitToPlay的屬性,它包含一個沒有受傷的玩家列表。我想要做的是爲球隊中的每個位置製作一個下拉框,並且只填充下拉框,讓球員適合播放名單中的球員,將他們的位置作爲他們的主要或次要位置。MVC如果對象具有某個屬性值,則顯示下拉框

我想知道的是如何使用html下拉框幫助器來顯示特定的對象。

非常感謝提前。

Ĵ

回答

0

我想你會通過每個位置要循環和內循環通過所有當前FitToPlay球員,如果不是他的第一或第二位的是目前通過位置環然後將他進去。 。在最後,如果有人插入創建一個下拉列表

因此,像..

//Loop through all the positions 
foreach (var position in Model.positions) 
{ 
    //Create a list for each position 
    List<SelectListItem> playersInPosition = new List<SelectListItem>(); 
    //Only loop through players with the current position as either primary or secondary 
    foreach(var player in Model.FitToPlay.Where(pl => pl.primary == position || pl.secondary == position)) 
    { 
    //Put this player into the list 
    playersInPosition.add(new SelectListItem { Text = player.name, Value = player.id}); 
    } 
    //If at least one fits the criteria make a drop down list from it 
    if(playersInPosition != null && playersInPosition.Count > 0) 
    { 
     @Html.DropDownList(position.name, playersInPosition); 
    } 
} 
相關問題