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!
Format-specific Action Results For example, returning JsonResult returns JSON-formatted data. Returning ContentResult or a string returns plain-text-formatted string data. An action isn't required to return any specific type.
Use JsonResult when you want to return raw JSON data to be consumed by a client (javascript on a web page or a mobile client). Use ActionResult if you want to return a view, redirect etc to be handled by a browser.
The Controller Action method will be called using jQuery POST function and JSON data will be returned back to the View using JsonResult class object. In this article I will explain with an example, how to use the JsonResult class object for returning JSON data from Controller to View in ASP.Net MVC.
Then, behind the scenes, it would put that JSON-compatible data (e.g. a dict ) inside of a JSONResponse that would be used to send the response to the client. But you can return a JSONResponse directly from your path operations.
{"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);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With