Search code examples
asp.net-mvchtml-helper

on change in dropdown helper?


I am using an enum to show drop down like

  @Html.DropDownList("MyType",EnumHelper.GetSelectList(typeof(ImageType)),"Select My Type", new { @class = "form-control", onChange = "myFunction(this)" })

I want to add a onchange function to this which accept two parameter one is the current element(this) and the dropdown value

   onChange(this,value)

this should give select tag, and valu should be what I selected what change should I ,make in my helper?


Solution

  • HTML Helper:

     @Html.DropDownList("MyType",EnumHelper.GetSelectList(typeof(ImageType)),"Select My Type", new { @class = "form-control", onChange = "myFunction(this)" })
    

    Javascript:

    <script>
    function myfunction(_this)
    {
       var selectedValue = $(_this).val();
    }
    </script>
    

    PS: you can access the element using _this. val() is going to give you the selected value.