我自定义了一个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'}]}";
result.Data = sResult; return result;
} public JsonResult GetAllUser1()
{
var result = new JsonResult(); List<Person> persons = new List<Person>()
{
new Person{ ID=1,UserName="zhangsan", Address="China"},
new Person{ ID=2,UserName="James", Address="USA"}
};
//datagrid数据源可正确识别。
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
result.Data = new { total=persons.Count,rows=persons}; return result;
} function GetAllPermission() {
$("#usertable").datagrid({
url: '/Home/GetAllUser1',
onLoadSuccess: LoadSuccess,
pageNumber: 1,
columns: [[
{ field: 'ID', title: '序号', hidden: false },
{ field: 'UserName', title: '模块名称' },
{ field: 'Address', title: '对应控制器' }
]]
});
} function LoadSuccess(data) {
} <table id="usertable" class="easyui-datagrid" fit="true" style="" data-options=" rownumbers: true, singleSelect:true, animate: true,collapsible: 'true', fitColumns: true,pagination:'true'">
<thead>
<tr>
<th data-options="field:'ID',width:40,align:'center'">
序号
</th>
<th data-options="field:'UserName',width:180,align:'center'">
用户名
</th>
<th data-options="field:'Address',width:180,align:'center'">
地质
</th>
</tr>
</thead>
</table>
easyuidatagrid
{
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'}]}";
result.Data = sResult; return result;
} public JsonResult GetAllUser1()
{
var result = new JsonResult(); List<Person> persons = new List<Person>()
{
new Person{ ID=1,UserName="zhangsan", Address="China"},
new Person{ ID=2,UserName="James", Address="USA"}
};
//datagrid数据源可正确识别。
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
result.Data = new { total=persons.Count,rows=persons}; return result;
} function GetAllPermission() {
$("#usertable").datagrid({
url: '/Home/GetAllUser1',
onLoadSuccess: LoadSuccess,
pageNumber: 1,
columns: [[
{ field: 'ID', title: '序号', hidden: false },
{ field: 'UserName', title: '模块名称' },
{ field: 'Address', title: '对应控制器' }
]]
});
} function LoadSuccess(data) {
} <table id="usertable" class="easyui-datagrid" fit="true" style="" data-options=" rownumbers: true, singleSelect:true, animate: true,collapsible: 'true', fitColumns: true,pagination:'true'">
<thead>
<tr>
<th data-options="field:'ID',width:40,align:'center'">
序号
</th>
<th data-options="field:'UserName',width:180,align:'center'">
用户名
</th>
<th data-options="field:'Address',width:180,align:'center'">
地质
</th>
</tr>
</thead>
</table>
easyuidatagrid
{
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding)
{
return new CustomJsonResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding
};
}
}
因为我有个需求是需要把自定义的json字符串返回客户端
EasyUI datagrid 里那个时间可以把拿到的字符串处理成json对象。
不是Webservice,,就是我自己定义的JsonResult,只是直接把字符串写入Response而已。
一个是MVC Jsonresult一个是我自定义的Jsonresult
直接访问这个地址跟访问正常json是一样的效果。 返回结果:
{"total":2,"rows":[{"ID":1,"UserName":"zhangsan","Address":"China"},{"ID":2,"UserName":"James","Address":"USA"}]}
{
response.ContentType = ContentType;
}
else
{
response.ContentType="application/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}*/