9

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

2 Answers 2

4

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.

5
  • lol, i just added that edit in and am reading the same post as we speak. :)
    – RPM1984
    Jan 21, 2011 at 1:11
  • Nice... I do the same thing sometimes!
    – Chaddeus
    Jan 21, 2011 at 1:28
  • You may also want to check out haacked.com/archive/2010/04/15/…
    – Chaddeus
    Jan 21, 2011 at 1:34
  • Grats, glad I could "help". The new MVC upgrades in v3 are awesome. I can't wait to dig into the JSON model binding feature as well.
    – Chaddeus
    Jan 21, 2011 at 3:01
  • It was awesome. I still had to construct the JSON object which isn't a big deal, but i love how model binding "just works". The convention-based nature of MVC is arguably it's best feature.
    – RPM1984
    Jan 21, 2011 at 8:12
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.

1
  • yes, im aware of that - and i do have that class. But with this method i still need to pass all the individual parameters, which is what im trying to avoid.
    – RPM1984
    Jan 21, 2011 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.