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>"
);
});
});
},