ASP.Net MVC Json Result: Parameters Passed to the Controller Method

I had a problem getting a controller method that returns a JsonResult to accept parameters passed using the JQuery getJSON method.

Im code works, it works fine when the second parameter ("data") of the getJSON method method is zero. But when I try to pass the value there, it seems that the controller method is not even called.

In this example, I just want to use an integer. A getJSON call that works fine looks like this:

$.getJSON("/News/ListNewsJson/", null, ListNews_OnReturn);

The controller method is as follows:

public JsonResult ListNewsJson(int? id)
{
    return Json(toReturn);
}

Having set a breakpoint in the ListNewsJson method, I see that this method is called when the getJSON data parameter is null, but when I replace it with a value, for example, 3:

$.getJSON("/News/ListNewsJson/", 3, ListNews_OnReturn);

... / . , ?

, , ( "/News/ListNewsJson/3" ).

+3
2

getJSON /, , {id: 3} id. , /News/ListNewsJson/? Id = 3, . Url, . , , . , - Url, .

var id = '3';
$.getJSON("/News/ListNewsJson/" + id, ListNews_OnReturn);
+3

, , MVC Url.Action()? .

$.getJSON('<%=Url.Action("ListNewsJson", "News") %>/' + id, ListNews_OnReturn);

, id ViewData, Url.Action() id, MVC URL. , javascript, javascript.

+1

Source: https://habr.com/ru/post/1704982/


All Articles