Asp.Net MVC3, returning success JsonResult

2019-01-14 14:40发布

问题:

I need to return JSON data that contain success value (true or false) also, it need to have result message too.

so I using Dictionary to contain data but when it return to Jason data, it contain ""(Quot).

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

it returns,

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

but I need,

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

Anybody know about this?

Thank you!

回答1:

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

is valid JSON. You can check it here. in jsonlint.com

You don't even need a Dictionary to return that JSON. You can simply use an anonymous variable like this:

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);
   });
});