Asp.Net MVC3 Returning JsonResult Success

I need to return JSON data containing the success value (true or false), I also need to have a message about the results.

so I use a dictionary to store data, but when it returns to Jason's data, it contains "" (Quot).

JsonResult = new Dictionary<string, string>();
JsonResult.Add("Success", "False");
JsonResult.Add("Message", "Error Message");
return Json(JsonResult);

he returns

{"Success":"False","Message":"Error Message"}

but i need

{Success:False,Message:"Error Message"} //with out "" (Quot)

Does anyone know about this?

Thank!

+5
source share
1 answer
{"Success":"False","Message":"Error Message"}

valid JSON . You can check it out here . at jsonlint.com

You don't even need a dictionary to return this JSON. You can simply use an anonymous variable as follows:

public ActionResult YourActionMethodName()
{
   var result=new { Success="False", Message="Error Message"};
   return Json(result, JsonRequestBehavior.AllowGet);
}

To access this data from your client, you can do this.

$(function(){
   $.getJSON('YourController/YourActionMethodName', function(data) {
      alert(data.Success);
      alert(data.Message);
   });
});
+30
source

All Articles