1

我正在使用 Json Result 来显示一个表格,当我显示结果时它工作正常。现在我想给它添加一个排序功能,所以我使用了 canSort:true 属性。但是现在,当我单击表格的标题进行排序时,我会在浏览器中得到下面的编码字符串,它似乎也已排序,但对其进行了某种编码,如下所示。

{"Data":"\u003ctable class=\"paramCustomDataTable\"\u003e\u003cthead\u003e\u003ctr class=\"customHead\"\u003e\u003cth scope=\"col\"\u003e\u003ca href=\"/Parameters/CustomData?id=7&sort=Name&sortdir=ASC\"\u003eName\u003c/a\u003e\u003c/th\u003e\u003cth scope=\"col\"\u003e\u003ca href=\"/Parameters/CustomData?id=7&sort=Value&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"}

我知道下面的代码中可能存在一些不一致之处,因为我必须删除版权问题的实际列。


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代码

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

2 回答 2

0

看看这个:

http://demos.telerik.com/aspnet-mvc/grid

于 2012-03-12T12:32:26.993 回答
0

在 ASP MVC 4 中,您可以执行下一个 IQueryable 支持

下一个很酷的特性是 IQueryable 支持。如果需要,您可以返回 IQueryable,而不是从 API 操作返回“普通”IEnumerable 对象。为什么?

记住我们使用 ASP.NET MVC 应用程序实现分页和排序的时代。这可能是有原因的,但它需要大量的手工工作。必须使用附加参数扩展操作,代码必须尊重这些参数并返回我们需要的确切数据部分。排序也是如此。在 Web API 中它要简单得多。

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

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

现在,如果 Web API 看到这样的方法,它将允许使用开放数据协议 (OData) 查询字符串参数进行访问。OData 支持以下查询:$filter、$orderby、$skip、$top。

现在,如果我提出请求:

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

我将收到 3 件顶级产品。或者类似的东西,

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

我将跳过 2 并休息 3。简而言之,有了 IQueryable 和 4 个 OData 查询参数,之前需要更多时间的事情就容易多了。

于 2012-03-22T15:24:03.040 回答