I've set up this test method on a controller to strip out any complication to it. Based off of all the results I've found from searching this should work. I'm not sure what I'm missing here.
public JsonResult test()
{
return Json(new { id = 1 });
}
This is the error I get.
Cannot implicitly convert type 'System.Web.Http.Results.JsonResult' to 'System.Web.Mvc.JsonResult'
In MVC
JsonResult
is inherited fromActionResult
which is in namespaceSystem.Web.Mvc
thats why you should make the Reference to
System.Web.Mvc.JsonResult
as::Try
return Json(new { id = 1 }, JsonRequestBehavior.AllowGet);
Try the following:
It seems that
Json
does not generate aSystem.Web.Mvc.JsonResult
which is expected as you are probablyusing System.Web.Mvc;
but aSystem.Web.Http.Results.JsonResult
.The more generic one should also work:
NOTE:
In my MVC controllers the
Json
method does return aSystem.Web.Mvc.JsonResult
. Are you inheriting from the defaultSystem.Web.Mvc.Controller
?You need to return the data through a model class rather than an anonymous class. Like:
Put this in your Using:
Then Your Action:
you should return a JsonResult instead of just Json