1

I have a json string in an action of MVC controller. I want to send it to view as a JSON object. How can I solve this?

public JsonResult Json()
{
    ... some code here ...
    string jsonString = "{\"Success\":true, \"Msg\":null}";
    // what should I do instead of assigning jsonString to Data. 
    return new JsonResult() { Data = jsonString, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

1 Answer 1

5
public ActionResult Json()
{
    ... some code here ...
    string jsonString = "{\"Success\":true, \"Msg\":null}";
    return Content(jsonString, "application/json");
}

But I would recommend you using objects instead of strings:

public ActionResult Json()
{
    ... some code here ...
    return Json(
        new 
        {
            Success = true,
            Msg = (string)null
        }, 
        JsonRequestBehavior.AllowGet
    );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.