tags:

views:

315

answers:

1

I'm using JQuery's getJSON method to retrieve some data from an MVC controller.

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult GetContacts(int? numberOf)
    {
        List<Contact> contacts =
            (numberOf != null && numberOf > 0) ?
                _provider.GetContacts(Convert.ToInt32(numberOf)):
                _provider.GetContacts();

        return Json(contacts);
    }

The idea, is that I can use this controller method to supply both all contacts, or a given number of contacts if "numberOf" is supplied.

The problem is that "numberOf" in my controller is always null when I send the GET request to "Contacts/GetContacts/5". However, if I send the GET request to "Contacts/GetContacts/?numberOf=5" it works as expected.

If it helps, here's the javascript method:

  getContacts: function(numberOf){
    var path = "/Contact/GetContacts/";
        path = (numberOf<=0) ? path : "/Contact/GetContacts/" + numberOf; 

    $.getJSON(path, null,
      function(json){
       $.each(json, function(){       
         $('tbody','#contacts').append(
             "<tr id=\"contact-"+ this.Id +"\">"
            +  "<td>"+ this.Id +"</td>"
            +  "<td>"+ this.FirstName +"</td>"
            +  "<td>"+ this.LastName +"</td>"
            + "</tr>"
         );
       });
    });
  },
+2  A: 

You probably have a routing issue - try applying either of these two fixes:

  1. (Easy but maybe a little ugly)
    Rename the numberOf parameter to id, to enable it to be picked up by the default route.

  2. (A little more work, but your code will look better - at least in this method)
    Add the following route to your route colleciton in global.asax.cs:

    routes.MapRoute(
        "ContactsRoute",
        "Contacts/GetContacts/{numberOf}",
        new { controller = "Contacts", action = "GetContacts", numberOf = null }
    );
    
Tomas Lycken
Dont think that is correct. If numberOf was 10, path would be: "/Contact/GetContact/10"...there is no reference to numberOf or id given this approach. Adding {numberOf} to the route table wont do anything. Please correct me if I'm wrong.
Amir
definitely looks like a routing problem if passing the query string does work.
scottm
I took the first approach first, because I had already defined a route. Your first suggestion worked, so you we're right about it being a routing issue. The root cause was that I had defined my route below the default route, instead of above it. So the request was getting routed to the default route and the controller couldn't map to the parameter because it was expecting 'Id' and not 'numberOf'.Thanks Tomas!
Kappers
aikr437: The reason adding `numberOf` to the route table works is that the name has to correspond to the name of the parameter in the Action Method. Kappers: You might want to look into Phil Haack's routing debugger - it can be really useful to determine which route is actually taking care of the request, and which other routes also match but are intercepted. See this blog post for details: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Tomas Lycken