JsonResult or Json: which to use?
Asked Answered
L

2

15

In ASP.NET MVC 3, which is more correct to use: Json() or new JsonResult()? Either returns the same result. Thanks for helping solve an office debate.

Labana answered 20/7, 2011 at 18:20 Comment(0)
C
17

Json() is just an extension method that actually returns a JsonResult object behind the scenes (rather than needing to call the constructor directly).

I almost always use the Extension Method myself. This keeps more in line with the other common return types from Action Methods like View(), PartialView(), etc.

I also make sure to create an extension method for any custom ActionResult types that I create. In the end it's a matter of personal preference.

Crim answered 20/7, 2011 at 18:26 Comment(1)
@BitFlipper both have been available since MVC 1 (msdn.microsoft.com/en-us/library/dd504936(v=VS.90).aspx )Want
S
1

If you a returning a large dataset as a data source for grid or other UI controls via Ajax, sometimes if this dataset is over 1000 records UI controls will not bind because of maximum Json length is not specified.

So instead of return Json(data),

you can do this: return new JsonResult(Data = data, MaxJsonLength = 50000);

Secund answered 1/9, 2022 at 17:41 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Soraya

© 2022 - 2024 — McMap. All rights reserved.