在 HTML.DropDownList Razor MVC 中处理 onchange 事件 [英] Handling onchange event in HTML.DropDownList Razor MVC

查看:58
本文介绍了在 HTML.DropDownList Razor MVC 中处理 onchange 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过简单的 HTML 处理一个带有选定值的 onchange 事件,如下所示:

I'm handling an onchange event with a selected value by simple HTML like this:

<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()

推荐答案

说明

您可以使用 DropDownList 方法的另一个重载.选择您需要的并传入具有 html 属性的对象.

Description

You can use another overload of the DropDownList method. Pick the one you need and pass in a object with your html attributes.

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

更多信息

  • MSDN - SelectExtensions.DropDownList 方法
  • 这篇关于在 HTML.DropDownList Razor MVC 中处理 onchange 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆