我自定义了一个JsonResult,直接返回一个字符串,字符串格式和系统返回的js格式相同。但是datagrid并没有识别,不知道是不是还需要设置别的,使用系统返回的json结果datagrid可以识别的。
public class CustomJsonResult:JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
HttpResponseBase response = context.HttpContext.Response;
if (!string.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType="application/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
response.Write(Data);
}
}
}
[HttpPost]
public JsonResult GetAllUser()
{
var result = new CustomJsonResult();
//这种写法是参考了系统JsonResult的返回结果的,而且在json编辑器里也可以正确识别。
string sResult = "{'total':2,'rows':[{'ID':1,'UserName':'James','Address':'USA'},{'ID':2,'UserName':'Zhangsan','Address':'China'}]}";