87,910
社区成员
发帖
与我相关
我的任务
分享
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>
/* if (!string.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType="application/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}*/
返回结果:
{"total":2,"rows":[{"ID":1,"UserName":"zhangsan","Address":"China"},{"ID":2,"UserName":"James","Address":"USA"}]}
Dictionary<string, object> json = new Dictionary<string, object>();
json.Add("total",xx);
json.Add("rows",xx);
return Json(json);
public class CustomerController:Controller
{
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding)
{
return new CustomJsonResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding
};
}
}