我在MVC控制器的动作中有一个json字符串。我想将其作为JSON对象发送到视图。我该如何解决这个问题?
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 };
}
答案 0 :(得分:5)
public ActionResult Json()
{
... some code here ...
string jsonString = "{\"Success\":true, \"Msg\":null}";
return Content(jsonString, "application/json");
}
但我建议您使用对象而不是字符串:
public ActionResult Json()
{
... some code here ...
return Json(
new
{
Success = true,
Msg = (string)null
},
JsonRequestBehavior.AllowGet
);
}