Razor - ドロップダウンリスト
Cotroller
var teamList = _teamService.GetTeamList().ToList();
teamList.Insert(0, new teamEntity()); // 先頭にブランクを追加
playerVm.TeamList = new SelectList(teamList, "team_id", "team_name");
ViewModel
public class PlayerViewModel
{
public IEnumerable<SelectListItem> TeamList { get; set; }
}
View
@using (Html.BeginForm("PlayerEdit", "Home", null, FormMethod.Post))
{
@Html.HiddenFor(x => x.Player_id)
<table>
<tr>
<th>@Html.LabelFor(x => x.Team_name)</th>
<td>@Html.DropDownListFor(x => x.Team_id, Model.TeamList)</td>
</tr>
</table>
<div><input type="submit" value="OK" /></div>
}
参考
- MSDN
- http://msdn.microsoft.com/ja-jp/library/dd410596(v=vs.100).aspx
- 「ドロップダウンリストを表示する時、Controller側でSelectListとSelectListItemのどちらを使えばよいか」
- http://miso-soup3.hateblo.jp/entry/20111101/1320160294