EasyUI datagrid不支持自定义的JsonResut 吗?

garfieldzf 2013-07-11 03:12:49
我自定义了一个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>
...全文
197 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
garfieldzf 2013-07-13
  • 打赏
  • 举报
回复
引用 12 楼 showbo 的回复:
测试了下。将贴出的数据存为txt后设置url转向这个txt没有问题。。可以显示数据,将设置项响应头的代码注释掉看看
/*            if (!string.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType="application/json";
            }
            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }*/
试了下,没有效果。
Go 旅城通票 2013-07-13
  • 打赏
  • 举报
回复
测试了下。将贴出的数据存为txt后设置url转向这个txt没有问题。。可以显示数据,将设置项响应头的代码注释掉看看
/*            if (!string.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType="application/json";
            }
            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }*/
garfieldzf 2013-07-13
  • 打赏
  • 举报
回复
引用 10 楼 showbo 的回复:
直接访问/Home/GetAllUser1看输出什么。。
直接访问这个地址跟访问正常json是一样的效果。

 返回结果:
{"total":2,"rows":[{"ID":1,"UserName":"zhangsan","Address":"China"},{"ID":2,"UserName":"James","Address":"USA"}]}
Go 旅城通票 2013-07-12
  • 打赏
  • 举报
回复
你那个是webservice吧,自己用firebug查看下返回什么内容就知道了,webservice返回的json格式{d:内容}这种的,内容依据返回的内容得到,返回json格式的字符串d的值就是string,不是json对象【jquery处理过后的】
garfieldzf 2013-07-12
  • 打赏
  • 举报
回复
引用 3 楼 showbo 的回复:
不要直接返回字符串,返回到客户端后得到的不是json对象,是json对象对应的字符串。。 返回一个类的示例,类构造要包含你这个{'total':2,'rows':[{'ID':1,'UserName':'James','Address':'USA'},{'ID':2,'UserName':'Zhangsan','Address':'China'}]}字符串定义的属性,而且返回json需要设置ajax请求头content-type为appliction/json
EasyUI datagrid 里那个时间可以把拿到的字符串处理成json对象。
garfieldzf 2013-07-12
  • 打赏
  • 举报
回复
引用 3 楼 showbo 的回复:
不要直接返回字符串,返回到客户端后得到的不是json对象,是json对象对应的字符串。。 返回一个类的示例,类构造要包含你这个{'total':2,'rows':[{'ID':1,'UserName':'James','Address':'USA'},{'ID':2,'UserName':'Zhangsan','Address':'China'}]}字符串定义的属性,而且返回json需要设置ajax请求头content-type为appliction/json
因为我有个需求是需要把自定义的json字符串返回客户端
Go 旅城通票 2013-07-12
  • 打赏
  • 举报
回复
直接访问/Home/GetAllUser1看输出什么。。
garfieldzf 2013-07-12
  • 打赏
  • 举报
回复
引用 8 楼 showbo 的回复:
你那个是webservice吧,自己用firebug查看下返回什么内容就知道了,webservice返回的json格式{d:内容}这种的,内容依据返回的内容得到,返回json格式的字符串d的值就是string,不是json对象【jquery处理过后的】
不是Webservice,,就是我自己定义的JsonResult,只是直接把字符串写入Response而已。 一个是MVC Jsonresult一个是我自定义的Jsonresult
  • 打赏
  • 举报
回复
LZ的问题我也遇到过 老是把JSON当作字符串来用、、、 3L 版主正解 我现在有用个工具类来处理json数据,避免json数据格式繁杂的问题
tony4geek 2013-07-11
  • 打赏
  • 举报
回复
 Dictionary<string, object> json = new Dictionary<string, object>();

            json.Add("total",xx);

            json.Add("rows",xx);

 

            return Json(json);
Go 旅城通票 2013-07-11
  • 打赏
  • 举报
回复
不要直接返回字符串,返回到客户端后得到的不是json对象,是json对象对应的字符串。。

返回一个类的示例,类构造要包含你这个{'total':2,'rows':[{'ID':1,'UserName':'James','Address':'USA'},{'ID':2,'UserName':'Zhangsan','Address':'China'}]}字符串定义的属性,而且返回json需要设置ajax请求头content-type为appliction/json
garfieldzf 2013-07-11
  • 打赏
  • 举报
回复
我重写了Controller,内部重写JsonResult,但是问题依然没有解决:

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
            };
        } 
    }

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧