开发者

How can I send a json string to view as a json object?

开发者 https://www.devze.com 2023-01-03 23:25 出处:网络
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?

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


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

精彩评论

暂无评论...
验证码 换一张
取 消