ASP.NET MVC 3 - 我们可以通过 jQuery AJAX 调用使用模型绑定吗?
如果我有以下 jQuery 函数(在外部文件中):
function getResults(field1, field2, field3) {
$.get('/Search/GetResults', { id: field1, type: field2, blah: field3 }, function(data) {
$('#target').html(data);
});
}
它本质上从表单中获取一堆字段,将它们发送到一个操作方法(返回 PartialViewResult),并将结果绑定到目标 div。
这是该操作方法:
[HttpGet]
public PartialViewResult GetResults(int id, int type, string blah)
{
var model = repository.GetResults(id, type, blah);
return PartialView("Results", model);
}
这里可以使用模型绑定吗?例如,我们可以这样做:
function getResults(someModel) {
$.get('/Search/GetResults', { model: someModel }, function(data) {
$('#target').html(data);
});
}
还有这个:
[HttpGet]
public PartialViewResult GetResults(SearchPreferences prefs)
{
var model = repository.GetResults(prefs);
return PartialView("Results", model);
}
或者我应该构造一个 JSON 对象并传递它?目前,这些值是通过单独的 jQuery DOM 调用检索的:
var field1 = $('#field1').val();
var field2 = $('#field2').val();
目标是减少/简化 jQuery 代码。我有所有这些调用来获取所有值,然后我需要将它们全部作为参数传递。
理想情况下,我只想传递一个对象。
有什么建议吗?
编辑:刚刚意识到我也许能够使用 ASP.NET MVC 3 中新的 JSON 模型绑定 功能。现在就阅读它......(随意回答在此期间提前)。
If i have the following jQuery function (in an external file):
function getResults(field1, field2, field3) {
$.get('/Search/GetResults', { id: field1, type: field2, blah: field3 }, function(data) {
$('#target').html(data);
});
}
Which essentially takes a bunch of fields from the form, sends them to an action method (which returns a PartialViewResult), and binds the result to a target div.
Here is that action method:
[HttpGet]
public PartialViewResult GetResults(int id, int type, string blah)
{
var model = repository.GetResults(id, type, blah);
return PartialView("Results", model);
}
Is it possible to use model-binding here? E.g can we do this:
function getResults(someModel) {
$.get('/Search/GetResults', { model: someModel }, function(data) {
$('#target').html(data);
});
}
And this:
[HttpGet]
public PartialViewResult GetResults(SearchPreferences prefs)
{
var model = repository.GetResults(prefs);
return PartialView("Results", model);
}
Or should i construct a JSON object and pass that? Currently those values are retrieved via individual jQuery DOM calls:
var field1 = $('#field1').val();
var field2 = $('#field2').val();
The goal is to reduce/simplify jQuery code. I have all those calls to grab all the values, then i need to pass them all as parameters.
Ideally i'd like to just pass one object.
Any recommendations?
EDIT: Just realized i may be able to use the new JSON Model Binding feature in ASP.NET MVC 3. Reading up on it now... (feel free to answer in advance in the meantime).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 ASP.NET MVC 3 中,是的!查看 此链接,来自 TheGu 本人。
In ASP.NET MVC 3, YES! Check out this link, from TheGu himself.
如果您创建一个包含要发送的属性的类,则默认模型绑定器将启动并将数据绑定到该类。在您的示例中,创建一个类:
然后在您的操作中可以是:
关键是让 $.get 数据中的名称与 SerachPreferences 类中的名称匹配。
If you create a class that contains the properties that you are sending over, the default model binder will kick in and bind the data to that class. In your example, create a class:
Then in your action it can be:
They key is to have the names in your $.get data match the names in your SerachPreferences class.