Brian Crist
Brian Crist

Reputation: 814

How to by pass value of dropdownlist to event onchange in MVC ?

I try with MVC and i don't know how to by pass value of dropdownlist to event onchange .

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

And this is function in script

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

I get this error for sample

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?)

So ,i think the problem when i try get value of dropdownlist.

Upvotes: 0

Views: 3552

Answers (1)

Abdelrahman M. Allam
Abdelrahman M. Allam

Reputation: 922

The this.value here refer to the context of server-side C#, you need to bind the this of the dropdown list on JavaScript onchange = "GetProduct(this, 123)"

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

and with your function, GetProduct do the following

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

and something else to pass the value directly like so

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


function GetProduct(selectedValue, number){

}

Upvotes: 1

Related Questions