Asp.net-Mvc

在 HTML.DropDownList Razor MVC 中處理 onchange 事件

  • January 23, 2012

我正在onchange通過簡單的 HTML 處理具有選定值的事件,如下所示:

<select onchange="location = this.value;">
        <option value="/product/categoryByPage?PageSize=15" selected="selected">15</option>
        <option value="/product/categoryByPage?PageSize=30" selected="selected">30</option>
        <option value="/product/categoryByPage?PageSize=50" selected="selected">50</option>
</select>

這樣做:

List<SelectListItem> items = new List<SelectListItem>();
string[] itemArray = {"15","30","50"};

for (int i = 0; i < itemArray.Count(); i++)
{
   items.Add(new SelectListItem 
   { 
       Text = itemArray[i], 
       Value = "/product/categoryByPage?pageSize=" + itemArray[i]
   });
}

ViewBag.CategoryID = items;
@Html.DropDownList("CategoryID")

我該如何onchange處理@Html.DropDownList()

描述

您可以使用該DropDownList方法的另一個重載。選擇一個你需要的,並用你的 html 屬性傳入一個對象。

樣本

@Html.DropDownList("CategoryID", null, new { @onchange="location = this.value;" })

更多資訊

引用自:https://stackoverflow.com/questions/8973037