[英]How to by pass value of dropdownlist to event onchange in MVC ?
我嘗試使用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?)
因此,我認為當我嘗試獲得dropdownlist的價值時的問題。
該this.value
這里指的服務器端的C#的背景下,你需要綁定的this
JavaScript的下拉列表中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){
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.