将列附加到jquery数据表的LINQ查询结果

时间:2014-11-02 10:16:39

标签: asp.net asp.net-mvc asp.net-mvc-5

我正在使用ASP.NET MVC 5和使用LINQ的EF 6 我有这个函数返回Jquery数据表的JSON结果。

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();
        }

我想要的是在json结果中追加一个包含任何字符串值的列,例如html按钮或锚标记等。

现在使用上面的代码我得到这样的结果..

{"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 个答案:

答案 0 :(得分: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()