Html.EditFor Onchange Event

I am developing an MVC4 application.
I want to change IsChanged in my model when model.ExternalVenderNo changes.

Below are the codes on the watch page

<div class="editor-field"> @Html.EditorFor(model => model.ExternalVenderNo, new { @onchange = "OnChangeEvent();" }) @Html.ValidationMessageFor(model => model.ExternalVenderNo) </div> 

and

  function OnChangeEvent() { alert("value is changed"); @{ Model.IsChanged = true; } } 

To check if OnChangeEvent is being called , I put an alert . But alert also does not work .
this means that OnChangeEvent is not being called.

I just want to change the IsChanged field to true when model.ExternalVenderNo changes.

Model has bool IsChanged

+6
source share
4 answers

Try to do the same with @Html.TextBoxFor

 @Html.TextBoxFor(model => model.ExternalVenderNo, new { onchange = "OnChangeEvent()" }) <script type="text/javascript"> function OnChangeEvent(){ alert("value is changed"); @Model.IsChanged = true; } </script> 

or leave jquery to handle the change event

 @Html.EditorFor(model => model.ExternalVenderNo) <script type="text/javascript"> $('#ExternalVenderNo').change(function(){ alert('Changed!'); @Model.IsChanged = true; }); </script> 
+8
source

The following will certainly work for you.

 $(document).ready(function() { $('#ExternalVenderNo').on("change", function() { alert('Changed!'); }); }); 
+3
source

Try adding to attributes

 @Html.EditorFor(model => Model.Lengthofside1, new { htmlAttributes = new { onchange = "OnChangeEvent(this)", @class = "form -control text-right" } }) 

It worked for me.

+2
source

try jquery for this

 $('#ExternalVenderNo').change(function(){ alert('Changed!'); }); 
+1
source

Source: https://habr.com/ru/post/956256/


All Articles