选择下拉列表时如何调用控制器方法

时间:2016-10-05 11:26:57

标签: asp.net-mvc

我在这里有代码。如何调用控制器方法。

<div class="uLeft">
    Select Institution:
    @Html.DropDownList("Sortby", new SelectListItem[]
    {
        new SelectListItem() { Text = "Option1", Value = "1" },
        new SelectListItem() { Text = "Option2", Value = "2" },
        new SelectListItem() { Text = "Option3", Value = "3" },
        new SelectListItem() { Text = "Option4", Value = "4" }
    },
    new { @onchange = "CallChangefunc(this.value)" })
</div>

2 个答案:

答案 0 :(得分:0)

您已经有了更改功能CallChangefunc(this.value)

接下来你将把这个函数声明为(post example) -

<script>
    function CallChangefunc(value) {
        $.post('http://localhost/{controller}/{action}', { sortBy: value }, function (data) {
            console.log(data)
        })
    }
</script>

答案 1 :(得分:0)

为您的下拉列表提供ID

@Html.DropDownList("Sortby", new SelectListItem[]
 {
         new SelectListItem() { Text = "Option1", Value = "1" }, 
         new SelectListItem() { Text = "Option2", Value = "2" }, 
         new SelectListItem() { Text = "Option3", Value = "3" }, 
         new SelectListItem() { Text = "Option4", Value = "4" }
 }, 
 new { id = "ddlInstitution" })

和你的jquery代码一样

$("#ddlInstitution").on('change', function () {

    var selValue = $('#ddlInstitution').val();

    $.ajax({
        type: "POST",
        url: "/{controller}/{action}",
        dataType: "json",
        data: { method parameter name : selValue },
        success: function (data) {

       },
       failure: function (data) {
            alert('oops something went wrong');
       }
   });
});