我正在使用 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);
});