使用Razor的MVC 4中的DropDownList

我正在尝试DropDownList在剃刀视图上创建一个。


有人可以帮我吗?


普通的HTML5代码:


<select id="dropdowntipo">

    <option value="Exemplo1">Exemplo1</option>

    <option value="Exemplo2">Exemplo2</option>

    <option value="Exemplo3">Exemplo3</option>

</select>

我尝试了这个:


@{

    var listItems = new List<ListItem> { 

        new ListItem { Text = "Exemplo1", Value = "Exemplo1" }, 

        new ListItem { Text = "Exemplo2", Value = "Exemplo2" }, 

        new ListItem { Text = "Exemplo3", Value = "Exemplo3" } 

    };

}


@Html.DropDownListFor(model => 

    model.tipo, 

    new SelectList(listItems), 

    "-- Select Status --"

)


浮云间
浏览 526回答 3
3回答

宝慕林4294392

@{&nbsp; &nbsp;List<SelectListItem> listItems= new List<SelectListItem>();&nbsp; &nbsp;listItems.Add(new SelectListItem&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text = "Exemplo1",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = "Exemplo1"&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp;listItems.Add(new SelectListItem&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text = "Exemplo2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = "Exemplo2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Selected = true&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp;listItems.Add(new SelectListItem&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text = "Exemplo3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value = "Exemplo3"&nbsp; &nbsp; &nbsp; &nbsp; });}@Html.DropDownListFor(model => model.tipo, listItems, "-- Select Status --")

沧海一幻觉

@{var listItems = new List<ListItem>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ListItem { Text = "Exemplo1", Value="Exemplo1" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ListItem { Text = "Exemplo2", Value="Exemplo2" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ListItem { Text = "Exemplo3", Value="Exemplo3" }&nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Html.DropDownList("Exemplo",new SelectList(listItems,"Value","Text"))
打开App,查看更多内容
随时随地看视频慕课网APP