Controller 页面:
List<Mall_TemplateModel> tList = wcf.GetMall_TemplateModelList(); //这里获取一个List
//然后定义一个ViewData,并且赋值List<SelectListItem>
ViewData["TemplateID"] = (tList == null) ?
new List<SelectListItem> { new SelectListItem { Text = "", Value = "" } }
: tList.Select(t => new SelectListItem
{
Text = t.TemplateName,
Value = t.ID.ToString()
});
cshtml 页面:
@Html.DropDownListFor(m => m.TemplateID, ViewData["TemplateID"] as SelectList, new { @class = "inputW100 input_Edit", style = "width: 85%;" })
或者:
Controller 页面:
List<CardStyleModel> listCardModel = wcf.GetCardStyleList(cardModel);//这里获取一个List
ViewBag.CardType = new SelectList(listCardModel, "StyleID", "CardName");
cshtml 页面:
@Html.DropDownList("CardType", "请选择") //这里的CardType 就是 ViewBag.CardType
或者
@Html.DropDownList("CardType", null, "请选择", new { @class = "input_Edit", style = "width:49.2%" })
或者
@Html.DropDownList("CardType", @ViewBag.CardType as SelectList, "请选择", new { @class = "input_Edit", style = "width:49.2%" })