ASP.NET MVC 3 - Can We Use Model Binding Over jQuery AJAX Calls? ASP.NET MVC 3 - Can We Use Model Binding Over jQuery AJAX Calls? json json

ASP.NET MVC 3 - Can We Use Model Binding Over jQuery AJAX Calls?


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

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.


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.