读书人

EasyUI datagrid不支持自定义的JsonRe

发布时间: 2013-07-16 22:38:05 作者: rapoo

EasyUI datagrid不支持自定义的JsonResut 吗?
我自定义了一个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>

easyui datagrid
[解决办法]
测试了下。将贴出的数据存为txt后设置url转向这个txt没有问题。。可以显示数据,将设置项响应头的代码注释掉看看

/*            if (!string.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType="application/json";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}*/

读书人网 >JavaScript

热点推荐