我正在使用简单的HTML处理带有选定值的onchange
事件,如下所示:
<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()
答案 0 :(得分:58)
您可以使用DropDownList
方法的另一个重载。选择你需要的那个并传入
具有html属性的对象。
@Html.DropDownList("CategoryID", null, new { @onchange="location = this.value;" })
答案 1 :(得分:5)
dknaack的方式对我不起作用,我也找到了这个解决方案:
.my-el img, .my-el canvas {
display: block;
width: 100%;
height: 100%;
max-width: inherit;
min-width: inherit;
max-height: inherit;
min-height: inherit;
}
,其中
@Html.DropDownList("Chapters", ViewBag.Chapters as SelectList,
"Select chapter", new { @onchange = "location = this.value;" })
在控制器中,您可以添加:
@Html.DropDownList(controlName, ViewBag.property + cast, "Default value", @onchange event)