如何将下拉列表的值传递给 MVC 中的事件 onchange?
How to by pass value of dropdownlist to event onchange in MVC ?
我尝试使用 MVC,但我不知道如何将下拉列表的值传递给事件 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?)
所以,我认为当我尝试获取下拉列表的值时出现问题。
这里的this.value
指的是server-sideC#的上下文,需要绑定下拉列表的this
到JavaScriptonchange = "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){
}
我尝试使用 MVC,但我不知道如何将下拉列表的值传递给事件 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?)
所以,我认为当我尝试获取下拉列表的值时出现问题。
这里的this.value
指的是server-sideC#的上下文,需要绑定下拉列表的this
到JavaScriptonchange = "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){
}