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

查看:690
本文介绍了在处理剃刀HTML.DropDownList 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>

做这件事是这样的:

Doing it like this:

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中的对象属性。

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方法

  • More Information

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

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