[asp.net]将列追加到 jquery 数据表的 LINQ 查询结果

发布时间: 2016/11/12 1:09:05
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我使用的 ASP.NET MVC 5,和 EF 6 与 LINQ 我有此函数所返回的 JSON 结果为 Jquery 数据表。

public ActionResult Index(jQueryDataTableParamModel param = null)
        {
            if (Request.IsAjaxRequest() && param != null)
            {

                var allCategories = _db.Categories.ToList();
                IEnumerable<Category> categories;
                var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
                var sortDirection = Request["sSortDir_0"]; // asc or desc
                Func<Category,string> orderingFunction = (c => sortColumnIndex==1? c.Name :
                    sortColumnIndex==2? c.SortOrder.ToString(): c.Status.ToString());

                if (!string.IsNullOrEmpty(param.sSearch))
                {
                    if(sortDirection == "desc"){
                    categories = (from category in allCategories
                                  where category.Name.ToLower().Contains(param.sSearch.ToLower())
                                  select category).OrderByDescending(orderingFunction).Skip(param.iDisplayStart).Take(param.iDisplayLength);
                        }
                    else
                    {
                        categories = (from category in allCategories
                                      where category.Name.ToLower().Contains(param.sSearch.ToLower())
                                      select category).OrderBy(orderingFunction).Skip(param.iDisplayStart).Take(param.iDisplayLength);
                    }
                }
                else
                {
                    if (sortDirection == "desc") { 
                    categories = (from category in allCategories
                                  select category).OrderByDescending(orderingFunction).Skip(param.iDisplayStart).Take(param.iDisplayLength);
                    }
                    else{
                        categories = (from category in allCategories
                                      select category).OrderBy(orderingFunction).Skip(param.iDisplayStart).Take(param.iDisplayLength);
                    }
                }
                return Json(new
                {
                    sEcho = param.sEcho,
                    iTotalRecords = categories.Count(),
                    iTotalDisplayRecords = categories.Count(),
                    aaData = (from category in categories
                              select new[] { category.CategoryID.ToString(), category.Name, category.SortOrder.ToString() }).ToArray()
                },
                                JsonRequestBehavior.AllowGet);
            }

            return View();
        }

想要的什么是附加包含任何字符串值,如 html 按钮或锚标记等的 json 结果中的列。

现在与上面的代码我得到这样的结果。

{"sEcho":"3","iTotalRecords":3,"iTotalDisplayRecords":3,"aaData":[["1","Computers","1"],["2","Laptops","4"],["3","Mobiles","3"]]}

但我想要用额外的列中的结果 aaData .示例︰

{"sEcho":"3","iTotalRecords":3,"iTotalDisplayRecords":3,"aaData":[["1","Computers","1","<a href='somelink'>ActionLink</a>"],["2","Laptops","4","<a href='somelink'>ActionLink</a>"],["3","Mobiles","3","<a href='somelink'>ActionLink</a>"]]}

我如何能达到这种效果。

解决方法 1:

更改此设置

 aaData = (from category in categories
                              select new[] { category.CategoryID.ToString(), category.Name, category.SortOrder.ToString() }).ToArray()

对此

 aaData = (from category in categories
                              select new[] { category.CategoryID.ToString(), category.Name, category.SortOrder.ToString(),"<a href='somelink'>ActionLink</a>" }).ToArray()
赞助商