如何处理MVC4 Razor中下拉列表的选定索引更改事件

时间:2014-03-12 13:39:49

标签: c# jquery asp.net-mvc asp.net-mvc-4 razor

我的视图页面中有以下控件。如何以非常简单的方式处理选定的更改事件?

@{
            List<SelectListItem> invoiceTypes= new List<SelectListItem>();
            invoiceTypes.Add(new SelectListItem
            {
                Text = "--Select One--",
                Value = "",
                Selected = true
            });
            invoiceTypes.Add(new SelectListItem
            {
                Text = "Form 8",
                Value = "Form 8"
            });
            invoiceTypes.Add(new SelectListItem
            {
                Text = "Form 8B",
                Value = "Form 8B"
            });
            invoiceTypes.Add(new SelectListItem
            {
                Text = "eBay",
                Value = "eBay"
            });
            invoiceTypes.Add(new SelectListItem
            {
                Text = "Snapdeal",
                Value = "Snapdeal"
            });
            }

            @Html.DropDownListFor(model => model.InvoiceType, invoiceTypes, new { @class = "dropdownlist"})

2 个答案:

答案 0 :(得分:1)

Html.DropDownListFor将呈现为select,如

<select class="dropdownlist">
    <option value="">--Select One--</option>
    <option value="eBay">eBay</option>
    <option value="Snapdeal">Snapdeal</option>
</select>

您可以处理change事件,例如

 $(".dropdownlist").change(function () {
     alert(this.value);
 });

DEMO

答案 1 :(得分:0)

You can use  OnChange Event of Jquery since its same as normal HTML Control:


   $(.dropdownlist).change(function()
{

  // Write your code here..
}

);

希望这会有所帮助..