MVC下拉框 一一 @Html.DropDownList使用

我们知道html的下拉框select,那么MVC的下拉框应该怎么写呢?下面做简单介绍

//后台数据
List<SelectListItem> itemList = new List<SelectListItem>();
hallBLL.GetAllEnableHall().ForEach(o =>
{
    SelectListItem item = new SelectListItem()
    {
        Value = o.HallID_int.ToString(),
        Text = o.HallName_nvarchar
    };
    itemList.Add(item);
});
SelectList select = new SelectList(itemList, "Value", "Text");
ViewBag.select = select;

 1、强类型@Html.DropDownListFor

@Html.DropDownListFor(model => model.HallID_int, ViewBag.select as SelectList, "==请选择展馆==", new { @class = "l-text-field", @style = "width:180px" })

2、非强类型

@Html.DropDownList("HallID_int", ViewBag.select as SelectList, "==请选择展馆==", new { @class = "l-text-field", @style = "width:180px" })

最后页面的html代码

<select class="l-text-field" data-val="true" data-val-number="字段 HallID_int 必须是一个数字。" id="HallID_int" name="HallID_int" style="width:180px">
  <option value="">==请选择展馆==</option>
  <option value="1">测试展厅数据2</option>
  <option value="2">俄罗斯展厅名称1</option>
</select>

 

所以,用好控件能少些很多代码。。T_T

 

posted @ 2015-12-03 15:17  notevar  阅读(11854)  评论(3编辑  收藏  举报