Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.EditFor Onchange Event

I am developing an MVC4 application.
I want to change IsChanged field in my Model, when model.ExternalVenderNo is changed.

Below are codes on View 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 whether OnChangeEvent is being called , i put alert. But alert is also not working.
it means OnChangeEvent is not beign called.

I just want to change IsChanged field to true, when model.ExternalVenderNo is changed.

Model has bool IsChanged

like image 255
user2739679 Avatar asked Oct 18 '13 14:10

user2739679


3 Answers

Try adding to attributes

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

This worked for me.

like image 100
Mark Avatar answered Nov 03 '22 06:11

Mark


The following will definitely work for you.

$(document).ready(function()
{
   $('#ExternalVenderNo').on("change", function()
   {
      alert('Changed!');
   });
});
like image 28
Dilip0165 Avatar answered Nov 03 '22 05:11

Dilip0165


Try 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>
like image 18
AthibaN Avatar answered Nov 03 '22 06:11

AthibaN