我正在处理一个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")
如何使用@ Html.DropDownList()处理onchange
解决方法
描述
您可以使用DropDownList方法的另一个重载。选择一个你需要和传入
一个具有html属性的对象。
样品
@Html.DropDownList("CategoryID",null,new { @onchange="location = this.value;" })
更多信息