I have a response object which I want to convert to Json, but somehow I can’t, propably beacuse of I cannot the framework too well.
I’m trying to convert a anonymous object to json but somehow it doesn’t go so well. I would preferably do it in JSON than just make a text/plain result.
In my controller:
ViewData["json"] = new { Ok = false };
In my view:
<%: ViewData["json"] %>
In my js-code (ajax-callback) I get back the following:
{ Ok = False }
Which ain’t what I am excepting. I want the
{ ok : false }
or else js doesn’t recognize the property in the object as a boolean.
Or are there some better way to push out json-data?
EDIT
I did a method in my controller with a JsonResult instead of Actionresult.
var obj = new
{
Ok = false,
Message = “”
};
return Json(obj);
And that’s the object I’m pushing out. And like this in js:
{"Ok":true,"Message":""}
C# 4.0 and Javascript goes along so well – it is beautiful!!!
You’ll need to use some library to format the data to json. I would recommend newtonsoft.json
http://james.newtonking.com/pages/json-net.aspx
You can also return a JsonResult in ASP.net mvc which will serialize your object for you.
But this ActionMethod will need to be called via AJAX to get the data.