ASP。NET MVC 3 -我们可以使用模型绑定来使用jQuery AJAX调用吗?

时间:2022-12-02 10:09:13

If i have the following jQuery function (in an external file):

如果我有以下jQuery函数(在外部文件中):

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.

它本质上是从表单中获取一系列字段,将它们发送到操作方法(该方法返回PartialViewResult),并将结果绑定到目标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:

是否可以在这里使用模型绑定?E。我们可以这样做吗?

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:

或者我应该构造一个JSON对象并传递它吗?目前,这些值是通过单个jQuery DOM调用获取的:

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.

其目的是减少/简化jQuery代码。我有所有这些调用来获取所有的值,然后我需要将它们作为参数传递。

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).

编辑:我刚刚意识到我可以在ASP中使用新的JSON模型绑定特性。净MVC 3。现在读一下……(同时,请提前回答。)

2 个解决方案

#1


4  

In ASP.NET MVC 3, YES! Check out this link, from TheGu himself.

在ASP。净MVC 3,是的!看看这个链接,来自TheGu自己。

ASP.NET MVC 3 now includes built-in support for posting JSON-based parameters from client-side JavaScript to action methods on the server. This makes it easier to exchange data across the client and server, and build rich JavaScript front-ends.

ASP。NET MVC 3现在包含了内置的支持,可以将基于json的参数从客户端的JavaScript发布到服务器上的操作方法。这使得在客户端和服务器之间交换数据更容易,并且构建丰富的JavaScript前端。

#2


1  

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:

如果您创建了一个包含您要发送的属性的类,那么默认的模型绑定器将介入并将数据绑定到该类。在您的示例中,创建一个类:

public class SearchPreferences 
{
  public int id { get; set; }

  public int type { get; set; }

  public string blah { get; set; }
}

Then in your action it can be:

那么在你的行动中可以是:

[HttpGet]
public PartialViewResult GetResults(SearchPreferences prefs)
{
   var model = repository.GetResults(prefs);
   return PartialView("Results", model);
}

They key is to have the names in your $.get data match the names in your SerachPreferences class.

它们的关键是在你的$中包含名称。获取与SerachPreferences类中的名称匹配的数据。

#1


4  

In ASP.NET MVC 3, YES! Check out this link, from TheGu himself.

在ASP。净MVC 3,是的!看看这个链接,来自TheGu自己。

ASP.NET MVC 3 now includes built-in support for posting JSON-based parameters from client-side JavaScript to action methods on the server. This makes it easier to exchange data across the client and server, and build rich JavaScript front-ends.

ASP。NET MVC 3现在包含了内置的支持,可以将基于json的参数从客户端的JavaScript发布到服务器上的操作方法。这使得在客户端和服务器之间交换数据更容易,并且构建丰富的JavaScript前端。

#2


1  

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:

如果您创建了一个包含您要发送的属性的类,那么默认的模型绑定器将介入并将数据绑定到该类。在您的示例中,创建一个类:

public class SearchPreferences 
{
  public int id { get; set; }

  public int type { get; set; }

  public string blah { get; set; }
}

Then in your action it can be:

那么在你的行动中可以是:

[HttpGet]
public PartialViewResult GetResults(SearchPreferences prefs)
{
   var model = repository.GetResults(prefs);
   return PartialView("Results", model);
}

They key is to have the names in your $.get data match the names in your SerachPreferences class.

它们的关键是在你的$中包含名称。获取与SerachPreferences类中的名称匹配的数据。