排序不使用Json Result给出编码输出

我正在使用Json Result来显示一个表,当我显示结果时它工作正常。 现在我想为它添加一个排序function,所以我使用了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); }); 

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

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

记住我们使用ASP.NET MVC应用程序实现分页和排序的时间。 这是可能的原因,但它需要大量的手工工作。 必须使用其他参数扩展操作,代码必须遵守这些参数并返回我们需要的确切数据部分。 排序相同的故事。 在Web API中它更简单。

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

 public IQueryable 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并rest3.简而言之,拥有IQueryable和4个OData查询参数,它可以更容易地完成之前需要更多时间的东西。