Md Ghouse Md Ghouse - 1 year ago 158
AngularJS Question

How to use @onchange in mvc dropdown

Please help me How can i use @onchange in mvc dropdownList

<div ng-controller="MyCntrl">
@Html.DropDownListFor(M => M.SaveHybridEmp.Cnt_Id, new SelectList(Model.GetCount, "Cnt_Id", "Cnt_Name"), "Selected Country",
new { @class = "form-control", @onchange = "hell(this.Cnt_Id);" })
</div>


Angularcode

app.controller('MyCntrl', function ($scope) {
$scope.hell = function () {
alert('change..');
}

Answer Source

Oh hell = no! That won't work. The onchange handler specified in the razor helper will bind to a javascript method specified in local scope. The angular scope is separated. You need to define a model for the select html control and use the ng-change directive.

Ng-Change Directive

Html Select

P.S.> for clean code rule, you might want to mix as little as posible razor helpers with angular directives

Note: I've assumed you use angular 1

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download