can somebody please tell me the difference if i do in my ActionResult
in this case
var transAcc= "SomeListHere";
var v = new JavaScriptSerializer().Serialize(transAcc);
return Content(v);
and
var transAcc= "SameListHere";
return Json(new {list=transAcc });
The difference is that in the first case you are not setting the Content-Type response header to
application/jsonwhile in the second this is done.In first case the response is plain text and looks like this:
And the
Content-Typeheader is set totext/htmlwhich is incorrect as this is not HTML. It’stext/plain. That’s not even a valid JSON string.In the second case it is a JSON string which looks like this:
Also in the first code example you are manually performing the JSON serialization which is plumbing code and should not be done in a controller and should be externalized in a custom ActionResult which is exactly what the creators of the ASP.NET MVC framework have don for you in the face of JsonResult which is your second code example.
Conclusion: if you want to send a JSON serialized representation of some model to the client always use the second approach.