vb.net 在 Html.TextBoxFor() onchange 事件上更新模型值而不提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19438284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update a model value on Html.TextBoxFor() onchange event without submitting
提问by aaronmallen
I have a textbox that I call in my view like so:
我有一个在我的视图中调用的文本框,如下所示:
@Html.TextBoxFor(Function(m) m.ActivityIDReturn)
What I'd like to be able to do is whenever a value is inserted into this textbox I would like it to update the model without having to hit any type of submit button. Several elements are populated in the view by the value of this textbox.
我希望能够做的是每当一个值插入到这个文本框中时,我希望它更新模型而不必点击任何类型的提交按钮。此文本框的值在视图中填充了多个元素。
So ideally the textbox would looks something like this:
所以理想情况下,文本框看起来像这样:
@Html.TextBoxFor(Function(m) m.ActivityIDReturn, new with {Key .onchange=UpdateModel()})
or
或者
@Html.TextBoxFor(Function(m) m.ActivityIDReturn, new with {Key .onchange= RETLog(ActivityIDReturn:=ActivityIDReturn)})
RETLog() being the function that creates the view in the firstplace.
RETLog() 是首先创建视图的函数。
So far the only way I've been able to accomplish this is:
到目前为止,我能够做到这一点的唯一方法是:
@Html.TextBoxFor(Function(m) m.ActivityIDReturn, New With {Key .onchange = "javascript:submit()"})
But I can only imagine what horrible side effects might come from this kind of thing.
但我只能想象这种事情可能会带来什么可怕的副作用。
EDIT: Exactly what I am trying to accomplish here
编辑:正是我想要在这里完成的
In My Model I have this:
在我的模型中,我有这个:
Public Property PS As RecordsTaskView
Get
Return GlobalVar.db.PS.RecordsTaskViews.Find(ActivityIDReturn)
End Get
Set(ByVal value As RecordsTaskView)
value = GlobalVar.db.PS.RecordsTaskViews.Find(ActivityIDReturn)
End Set
End Property
In my view I have This:
在我看来,我有这个:
@Html.LabelFor(Function(m) m.ActivityIDReturn)
@Html.TextBoxFor(Function(m) m.ActivityIDReturn, New With {Key .onchange="javascript:submit()"})
@Html.DisplayFor(Function(m) m.PS.RefActionID)
@Html.DisplayFor(Function(m) m.PS.QutDesc)
@Html.DisplayFor(Function(m) m.PS.TaskDesc)
@Html.DisplayFor(Function(m) m.PS.CltCode)
@Html.DisplayFor(Function(m) m.PS.CltDesc)
@Html.DisplayFor(Function(m) m.PS.BenIDin)
Basically all of these DisplayFor are populated by the value of this textbox. I would like these DisplayFor values to populate when the user enters a value in the textbox without the user having to refresh the page, or hitting enter or hitting any buttons.
基本上所有这些 DisplayFor 都由这个文本框的值填充。我希望这些 DisplayFor 值在用户在文本框中输入值时填充,而无需用户刷新页面,或按 Enter 或按任何按钮。
Here is a view at my code in it's entirety: https://gist.github.com/aaronmallen/7042328
这是我的代码的完整视图:https: //gist.github.com/aaronmallen/7042328
回答by Matt Bodily
you need to use a jquery ajax call.
您需要使用 jquery ajax 调用。
$('#ActivityIDReturn').change(function(){
$.ajax({
url: '@Url.Action("Action", "Controller")',
type: 'post',
data: {
Text: $('#TextField').val()
},
success: function (_result) {
$('#TaskDesc').val(_result.foo);
}
});
});
I haven't done for's like you have in your fiddler. I have always done them with lamda's something like
我没有像你在你的小提琴手里那样做。我总是用 lamda 之类的东西来完成它们
@Html.DisplayFor(Function(m) m.TaskDesc)
which will auto generate an id of TaskDesc. You will need to look at your generated Html to make sure that the id's you call in your jquery match.
这将自动生成 TaskDesc 的 id。您需要查看生成的 Html 以确保您在 jquery 中调用的 id 匹配。
and a controller method
和控制器方法
<HttpPost()> _
Public Function UpdateFields(Text As String) As ActionResult
Dim model as MODELNAME = //query the database
Return Json(New With { .foo = model.bar, .baz = model.baz });
End Function
this will fire every time the field changes so if they type a word with 10 letters it will fire 10 times. Hopefully this helps
这将在每次字段更改时触发,因此如果他们输入一个包含 10 个字母的单词,它将触发 10 次。希望这会有所帮助

