2014-04-10 54 views
0

我的下拉代碼是目前的下方,我知道我需要的是這種格式:我想降下來轉換爲HTML helper.dropdownfor()

IEnumerable<SelectListItem> selectlist 

我現在什麼使用此代碼,我需要它具有以下信息轉換爲IEnumerable的selectlistitem

<select id="paperStyle" name="paperStyleList"> 
     <option selected="selected" value="">Select Style</option> 
     <option value="APA">APA</option> 
     <option value="Chicago">Chicago</option> 
     <option value="Harvard">Harvard</option> 
     <option value="MLA">MLA</option> 
     <option value="Oxford">Oxford</option> 
</select> 
+1

此鏈接可能的Userful http://stackoverflow.com/questions/867117/how-to-add-static-list-of-items-in-mvc-html-dropdownlist – faraaz

回答

0

試試這個列表,

控制器

[HttpGet] 
     public ActionResult CustomerInfo() 
     { 

      List<SelectListItem> items = new List<SelectListItem>(); 
      items.Add(new SelectListItem() { Text = "Chicago", Value = "Chicago", Selected = false }); 
      items.Add(new SelectListItem() { Text = "APA", Value = "APA", Selected = false }); 
      items.Add(new SelectListItem() { Text = "Harvard", Value = "Harvard", Selected = false }); 
      items.Add(new SelectListItem() { Text = "Oxford", Value = "Oxford", Selected = false }); 

      ViewBag.paperStyle= new SelectList(items, "Value", "Text"); 
      return View(); 
     } 

查看

@Html.DropDownList("CustomerId", (SelectList)ViewBag.paperStyle, "--Select--") 
1

這樣的事情應該工作:

@Html.DropDownListFor(m => m.paperStyle, Model.paperStyleList.Select(item => new SelectListItem { Value = item, Text = item }), "Select Style") 

或本:

@Html.DropDownListFor(m => m.paperStyle, new SelectList(Model.paperStyleList), "Select Style") 

其中paperStyle是你的模型和paperStyleList的屬性是爲此事IEnumerable類型或List的值(APA,芝加哥等)