排序与JSON结果的工作给予编码输出 [英] Sorting not working with Json Result giving encoded output

查看:181
本文介绍了排序与JSON结果的工作给予编码输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSON结果显示表,它工作正常,当我显示结果。现在我想排序功能添加到它,所以我用了canSort:true属性。但现在whenver我在桌子上的标题点击的排序发生我得到的下面的编码串在浏览器中,似乎它太排序但某种编码完成它,它是如下面



  {数据:\\\\\\\ ?u003ctr类= \customHead\\\\>\\\\\\Name\\\\\\\\\\\\DataValue\\\\\\\\\Delete\\\\\\\\\\\\\\\\\\Newdata \\\\\\123456\\\\\\\\\Delete\\\\\\\\\\\\\\\} 

我知道,因为我不得不删除实际列版权问题有可能是在下面的代码一些不一致的地方。






  C#代码
[CacheControl(HttpCacheability.NoCache),AcceptVerbs(HttpVerbs.Get)
公共JsonResult GetMyData(INT ID){
VAR的结果= _myRepository.GetmyDataWithId(ID).ToList();
VAR电网=新的WebGrid(因此,rowsPerPage:5,canSort:真正的);
VAR htmlString = grid.GetHtml(
柱:grid.Columns(
grid.Column(姓名,姓名),
grid.Column(值, DataValue),
));
返回JSON(新
{
数据= htmlString.ToHtmlString()
}
,JsonRequestBehavior.AllowGet);
}






JavaScript代码

  $ .getJSON(@ Url.Action(GetMyData)',{ID:1},功能(结果){
变种customDataList = $('#格');
customDataList.empty();
customDataList.append(result.Data);
});


解决方案

在ASP MVC 4,你可以做下一个
IQueryable的支持



接下来很酷的功能是IQueryable的支持。如果你需要,而不是返回从API行动普通IEnumerable的对象,你可能会返回IQueryable的。为什么?



记住我们实现分页和放大器的时代;在ASP.NET MVC应用程序排序。这是可能的原因,但它需要大量的手工工作。操作必须与其它参数进行扩展,代码必须尊重这些参数和返回数据的确切部分,我们需要对。同样的故事与排序。在网页API就简单多了。



更改签名和返回类型为IQueryable的。

 公开的IQueryable<产品与GT;获得()
{
返回_storage.AsQueryable();
}

现在,如果网络API看到这样的方法,它将允许访问与开放数据协议(OData的)查询字符串参数。的OData提供了以下查询的支持:$过滤器,$排序依据,$跳过,$顶部



现在,如果我的要求去做:

  **的http://本地主机:5589 / API /产品顶部$ = 3 ** 

我将获得3顶级产品。 ?或类似的东西,

  **的http://本地主机:5589 / API /产品$跳过= 2及$顶部= 3 ** 

我会跳过2,采取休息3总之,有IQueryable的和4的OData查询参数它更容易做之前需要更多时间的东西。


I am using Json Result to show a table, it is working fine when I show the result. Now I wanted to add a sort feature to it, so I used the canSort:true property. But now whenver I click on the header of the table for the sort to happen I get the below encoded string in my browser, it seems it is sorted too but some kind of encoding is done to it, it is as below.

{"Data":"\u003ctable class=\"paramCustomDataTable\"\u003e\u003cthead\u003e\u003ctr class=\"customHead\"\u003e\u003cth scope=\"col\"\u003e\u003ca href=\"/Parameters/CustomData?id=7&amp;sort=Name&amp;sortdir=ASC\"\u003eName\u003c/a\u003e\u003c/th\u003e\u003cth scope=\"col\"\u003e\u003ca href=\"/Parameters/CustomData?id=7&amp;sort=Value&amp;sortdir=DESC\"\u003eDataValue\u003c/a\u003e\u003c/th\u003e\u003cth scope=\"col\"\u003eDelete\u003c/th\u003e\u003c/tr\u003e\u003c/thead\u003e\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003eNewdata\u003c/td\u003e\u003ctd\u003e123456\u003c/td\u003e\u003ctd\u003e\u003ca href=\u0027delete/5\u0027\u003eDelete\u003c/a\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\u003c/table\u003e"}

I know there might be some inconsistencies in the code below as I had to remove the actual columns for copyright issues.


C# code
[CacheControl(HttpCacheability.NoCache), AcceptVerbs(HttpVerbs.Get)]
 public JsonResult GetMyData(int id)      {
            var result = _myRepository.GetmyDataWithId(id).ToList();
            var grid = new WebGrid(result, rowsPerPage: 5, canSort:true);
            var htmlString = grid.GetHtml(
                                          columns: grid.Columns(
                                              grid.Column("Name", "Name"),
                                              grid.Column("Value", "DataValue"),                                              
                                              ));
        return Json(new
        {
           Data = htmlString.ToHtmlString()
        }
        , JsonRequestBehavior.AllowGet);
    }


Javascript Code

 $.getJSON('@Url.Action("GetMyData")', { id: 1 }, function (result) {
                var customDataList = $('#grid');
                customDataList.empty();
                customDataList.append(result.Data);
            });

解决方案

in ASP MVC 4 you can do next IQueryable Support

Next cool feature is IQueryable support. If you need to, instead of returning "plain" IEnumerable objects from the API action, you might return IQueryable. Why?

Remember the times we implemented paging & sorting with ASP.NET MVC application. It was possible of cause, but it required a lot of manual job. Actions had to be extended with additional parameters, code have to respect those parameters and return exact portion of data we require to. The same story with sorting. In Web API it much simpler.

Change the signature and return type to IQueryable.

public IQueryable<Product> Get()
{
    return _storage.AsQueryable();
}

Now, if Web API sees the method like that, it will allow to access with with Open Data Protocol (OData) query string parameters. OData provides support for following queries: $filter, $orderby, $skip, $top.

Now, if I do the request:

**http://localhost:5589/api/products?$top=3**

I will receive, 3 top products. Or something like,

**http://localhost:5589/api/products?$skip=2&$top=3**

I will skip 2 and take rest 3. In short, having IQueryable and 4 OData query parameters it's much more easy to do the stuff required more time before.

这篇关于排序与JSON结果的工作给予编码输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆