html.dropdownlist的onchange事件

use*_*193 36 html c# asp.net asp.net-mvc html.dropdownlistfor

我正在尝试为dropdownlist触发onchange事件的动作方法,如何在不使用jquery onchange的情况下执行此操作.

@Html.DropDownList("Sortby", 
                   new SelectListItem[] 
                   { 
                       new SelectListItem() { Text = "Newest to Oldest", Value = "0" }, 
                       new SelectListItem() { Text = "Oldest to Newest", Value = "1" }})
Run Code Online (Sandbox Code Playgroud)

谢谢

Kar*_*sla 48

如果你不想要jquery那么你可以使用javascript:

@Html.DropDownList("Sortby", new SelectListItem[] 
{ 
     new SelectListItem() { Text = "Newest to Oldest", Value = "0" }, 
     new SelectListItem() { Text = "Oldest to Newest", Value = "1" }},
     new { @onchange="callChangefunc(this.value)" 
});

<script>
    function callChangefunc(val){
        window.location.href = "/Controller/ActionMethod?value=" + val;
    }
</script>
Run Code Online (Sandbox Code Playgroud)

  • @BurakKarakuş...获取选定的文本...代码会有所不同...请参阅[fiddle](http://jsfiddle.net/vz2bdsbh/) (2认同)

Moo*_*ons 28

你可以这样做

@Html.DropDownList("Sortby", new SelectListItem[] { new SelectListItem() 
  { 

       Text = "Newest to Oldest", Value = "0" }, new SelectListItem() { Text = "Oldest to Newest", Value = "1" } , new
       {
           onchange = @"form.submit();"
       }
})
Run Code Online (Sandbox Code Playgroud)


San*_*isy 18

@Html.DropDownListFor(m => m.State,
              new SelectList(ViewBag.StateList, "StateId", "StateName"),
              "Select state",
              new { @class = "form-control", @onchange="FillCity()" })



function FillCity() {
    var stateId = $('#State').val();
    $.ajax({
        url: '/Employees/FillCity',
        type: "GET",
        dataType: "JSON",
        data: { State: stateId},
        success: function (cities) {                    
            $("#City").html(""); // clear before appending new list 
            $.each(cities, function (i, city) {
                $("#City").append(
                    $('<option></option>').val(city.CityId).html(city.CityName));
            });
        }
    });
  }
Run Code Online (Sandbox Code Playgroud)


The*_*her 5

如果要将值传递给action方法,则可以尝试此操作.

@Html.DropDownList("Sortby", new SelectListItem[] { new SelectListItem() { Text = "Newest to Oldest", Value = "0" }, new SelectListItem() { Text = "Oldest to Newest", Value = "1" }},new { onchange = "document.location.href = '/ControllerName/ActionName?id=' + this.options[this.selectedIndex].value;" })
Run Code Online (Sandbox Code Playgroud)

在没有参数传递的情况下删除查询字符串.