本文介绍了Html.EditFor Onchange事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个MVC4应用程序。

我希望在 model.ExternalVenderNo 更改时更改模型中的 IsChanged 字段。

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>

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

检查 OnChangeEvent 被叫,我把警告
警告无效

表示 OnChangeEvent 未调用。

To check whether OnChangeEvent is being called , i put alert.But alert is also not working.
it means OnChangeEvent is not beign called.

model.ExternalVenderNo 发生变化时,我只想将 IsChanged 字段更改为true。

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

模型 bool IsChanged

推荐答案

尝试使用 @ Html.TextBoxFor

@Html.TextBoxFor(model => model.ExternalVenderNo, new { onchange = "OnChangeEvent()" })

<script type="text/javascript">
 function OnChangeEvent(){
    alert("value is changed");
    @Model.IsChanged = true;
 }
</script>

或让Jquery处理变更事件

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>

这篇关于Html.EditFor Onchange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:19