Html.DropDownList()的用法 ( Asp.Net MVC)

來源:互聯網
上載者:User

Html.DropDownList()賦預設值:

頁面代碼如下:

  <% 
                List<SelectListItem> list = new List<SelectListItem> {

                new SelectListItem { Text = "啟用", Value = "0",Selected = true},

                new SelectListItem { Text = "禁用", Value = "1" } };
  %>//list儲存dropdownlist的預設值
 <%=Html.DropDownList("state",list,Model.state) %>  //state為實體的屬性,預設選中"啟用"

Html.DropDownList()從資料庫讀取值:

頁面代碼如下:

<%= Html.DropDownList("Category", ViewData["Categories"] as SelectList,"--請選擇--",new { @class = "my-select-css-class" } )%>

Controllers代碼:

public ActionResult Create() 

{
        List<Category> categories = categoryService.GetAll();
        ViewData["Categories"] = new SelectList(categories, "Id", "Name");
        return View();
}

 

 

  • 原型一:

public static string DropDownList(this HtmlHelper htmlHelper, string name)

{

     IEnumerable<SelectListItem> selectData = htmlHelper.GetSelectData(name);

     return htmlHelper.SelectInternal(null, name, selectData, true, false, null);

}

 

 

 

第一種方式:
List<SelectListItem> items = new List<SelectListItem>();

items.Add(new SelectListItem() { Text = "001", Value = "1", Selected = false });

items.Add(new SelectListItem() {Text = "002", Value = "2", Selected = false });

ViewData["items"] = items;

 

簡化後:

 

 

var items = new List<SelectListItem>()

{

    (new SelectListItem() {Text = "001", Value = "1", Selected = false}),

    (new SelectListItem() {Text = "002", Value = "2", Selected = false})

};

將items值給ViewData:

ViewData["items"] = items;

 

在aspx中這樣使用:

<%= Html.DropDownList("items") %>

 

產生的程式碼中,items將作為<select>標籤的name和id值。

 

  • 原型二:
public static string DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
{
return htmlHelper.DropDownList(name, selectList, null);
}
使用方法:

<%= Html.DropDownList("items", new List<SelectListItem>
{
    (new SelectListItem() {Text = "001", Value = "1", Selected = false}),
    (new SelectListItem() {Text = "002", Value = "2", Selected = false})
})%>

在這裡,不需要ViewData傳入值,第一個參數items作為標籤的name和id的值。items也可以是任意的字串。

  • 原型三
public static string DropDownList(this HtmlHelper htmlHelper, string name, string optionLabel)
{
IEnumerable<SelectListItem> selectData = htmlHelper.GetSelectData(name);
return htmlHelper.SelectInternal(optionLabel, name, selectData, true, false, null);
}

使用方法和第一種原型相同,string optionLabel作為一個預設的空的選項。這樣可以完成加入不需要選取任何選項的情境。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.