我自定义了一个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

解决方案 »

  1.   

    我重写了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
                };
            } 
        }
      

  2.   

    本帖最后由 showbo 于 2013-07-11 15:34:54 编辑
      

  3.   

     Dictionary<string, object> json = new Dictionary<string, object>();            json.Add("total",xx);            json.Add("rows",xx);             return Json(json);
      

  4.   

    LZ的问题我也遇到过  老是把JSON当作字符串来用、、、  3L  版主正解我现在有用个工具类来处理json数据,避免json数据格式繁杂的问题
      

  5.   


    因为我有个需求是需要把自定义的json字符串返回客户端
      

  6.   


    EasyUI datagrid 里那个时间可以把拿到的字符串处理成json对象。
      

  7.   

    你那个是webservice吧,自己用firebug查看下返回什么内容就知道了,webservice返回的json格式{d:内容}这种的,内容依据返回的内容得到,返回json格式的字符串d的值就是string,不是json对象【jquery处理过后的】
      

  8.   


    不是Webservice,,就是我自己定义的JsonResult,只是直接把字符串写入Response而已。
    一个是MVC Jsonresult一个是我自定义的Jsonresult
      

  9.   

    直接访问/Home/GetAllUser1看输出什么。。
      

  10.   


    直接访问这个地址跟访问正常json是一样的效果。 返回结果:
    {"total":2,"rows":[{"ID":1,"UserName":"zhangsan","Address":"China"},{"ID":2,"UserName":"James","Address":"USA"}]}
      

  11.   

    测试了下。将贴出的数据存为txt后设置url转向这个txt没有问题。。可以显示数据,将设置项响应头的代码注释掉看看/*            if (!string.IsNullOrEmpty(ContentType))
                {
                    response.ContentType = ContentType;
                }
                else
                {
                    response.ContentType="application/json";
                }
                if (ContentEncoding != null)
                {
                    response.ContentEncoding = ContentEncoding;
                }*/