如何通过将dropdownlist的值传递给MVC中的事件onchange?

时间:2018-03-10 12:10:20

标签: javascript jquery model-view-controller onchange html.dropdownlistfor

我尝试使用MVC,我不知道如何通过将dropdownlist的值传递给事件onchange。

@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(" + this.value + ", 123)" })

这是脚本中的功能

 function GetProduct(productid, categoryid) {
////do something
}

我为示例

收到此错误
Compiler Error Message: CS1061: 'ASP._Page_Areas_Users_Views_Permit_GrandPermit_cshtml' does not contain a definition for 'value' and no extension method 'value' accepting a first argument of type 'ASP._Page_Areas_Users_Views_Permit_GrandPermit_cshtml' could be found (are you missing a using directive or an assembly reference?)

所以,我认为当我尝试获取下拉列表的值时问题。

1 个答案:

答案 0 :(得分:1)

这里的this.value是指服务器端C#的上下文,您需要绑定JavaScript this下拉列表的onchange = "GetProduct(this, 123)"

@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(this, 123)" })

使用您的功能,GetProduct执行以下操作

function GetProduct(drp, number){
     $(drp).val() 
     //  or
     drp.value
}

和其他东西一样直接传递值

@Html.DropDownList("cblist_id", (IEnumerable<SelectListItem>)ViewBag.listproduct, new { size = 5, @class = "form-control", onchange = "GetProduct(this.value, 123)" })


function GetProduct(selectedValue, number){

}