mvc - jQuery ajax datatable actionlink

时间:2013-03-26 09:53:46

标签: asp.net-mvc datatables

我有一个数据表,其中数据(来自数据库)正在用ajax填充,我还想要一个带有“Details”的新tablerow来显示所选项的更多细节,但该表只允许来自数据库的数据。这是视图

 <script language="javascript" type="text/javascript">

     $(document).ready(function () {

         $('#myDataTable').dataTable({
             "bProcessing": false,
             "bServerSide": true,
             "sAjaxSource": 'AjaxDataProvider',
             "aoColumns": [
                                     {
                                         "sName": "ID",


                                     },
                                     { "sName": "Student_naam" },
                                     { "sName": "klas" },
                                     { "sName": "adres" },
                                     { "sName": "woonplaats" },
                                     { "sName": "details" }
             ]

         })
     });

    </script>

我下面有一张桌子,里面有一些代码,包括:

<td>
            @Html.ActionLink("Details", "Index", "StudentGegevens", new {id = item.studentnr})
        </td>

这是我的控制器

public ActionResult AjaxDataProvider(JQueryDataTableParamModel param)
    {
        var allStudents = hoi.STUDENT.ToList();
        IEnumerable<STUDENT> filteredStudents;
        if (!string.IsNullOrEmpty(param.sSearch))
        {
            //Used if particulare columns are filtered 
            var roepnaamFilter = Convert.ToString(Request["sSearch_1"]);
            var adresFilter = Convert.ToString(Request["sSearch_2"]);
            var woonplaatsFilter = Convert.ToString(Request["sSearch_3"]);
            var klasFilter = Convert.ToString(Request["sSearch_4"]);

            //Optionally check whether the columns are searchable at all 
            var isNameSearchable = Convert.ToBoolean(Request["bSearchable_1"]);
            var isAddressSearchable = Convert.ToBoolean(Request["bSearchable_2"]);
            var isTownSearchable = Convert.ToBoolean(Request["bSearchable_3"]);
            var isClassSearchable = Convert.ToBoolean(Request["bSearchable_4"]);

            filteredStudents = hoi.STUDENT.ToList()
               .Where(c => isNameSearchable && c.roepnaam.ToLower().Contains(param.sSearch.ToLower())
                           ||
                           isAddressSearchable && c.adres.ToLower().Contains(param.sSearch.ToLower())
                           ||
                           isTownSearchable && c.woonplaats.ToLower().Contains(param.sSearch.ToLower())
                           ||
                           isClassSearchable && c.klas.ToLower().Contains(param.sSearch.ToLower()));
        }
        else
        {
            filteredStudents = allStudents;
        }

        var isNameSortable = Convert.ToBoolean(Request["bSortable_1"]);
        var isAddressSortable = Convert.ToBoolean(Request["bSortable_2"]);
        var isTownSortable = Convert.ToBoolean(Request["bSortable_3"]);
        var isClassSortable = Convert.ToBoolean(Request["bSortable_4"]);
        var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
        Func<STUDENT, string> orderingFunction = (c => sortColumnIndex == 1 && isNameSortable ? c.roepnaam :
                                                      sortColumnIndex == 2 && isClassSortable ? c.klas :
                                                      sortColumnIndex == 3 && isAddressSortable ? c.adres :
                                                      sortColumnIndex == 4 && isTownSortable ? c.woonplaats :
                                                      "");

        var sortDirection = Request["sSortDir_0"]; // asc or desc
        if (sortDirection == "asc")
        {
            filteredStudents = filteredStudents.OrderBy(orderingFunction);
        }
        else
        {
            filteredStudents = filteredStudents.OrderByDescending(orderingFunction);
        }

        var displayedStudents = filteredStudents.Skip(param.iDisplayStart).Take(param.iDisplayLength);
        var result = from c in displayedStudents select new[] { Convert.ToString(c.studentnr), c.roepnaam, c.klas, c.adres, c.woonplaats, "Here is the thing I dont know what to fill in" };
        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = allStudents.Count(),
            iTotalDisplayRecords = filteredStudents.Count(),
            aaData = result
        },
                    JsonRequestBehavior.AllowGet);
    }

还有一些公众舆论

public class JQueryDataTableParamModel
    {   
        public string sEcho { get; set; }

        public string sSearch { get; set; }

        public int iDisplayLength { get; set; }

        public int iDisplayStart { get; set; }

        public int iColumns { get; set; }

        public int iSortingCols { get; set; }

        public string sColumns { get; set; }


    }

我想知道我应该在var Result中添加什么。

一切都正确显示,除了链接,我尝试了类似Url.Action的东西,但没有运气,它没有给回链接。

2 个答案:

答案 0 :(得分:1)

不太确定您要显示哪种详细数据,但您可以查看方法: fnrender or mRender ,下面是一个示例。

"aoColumns": [
                                     {
                                         "sName": "ID",


                                     },
                                     { "sName": "Student_naam" },
                                     { "sName": "klas" },
                                     { "sName": "adres" },
                                     { "sName": "woonplaats" },
                                     { "sName": "details",
  "mRender": function ( data, type, full ) {
        return '<a href="'+data+'">Download</a>';
      } }
             ]

答案 1 :(得分:0)

对不起,我是个笨蛋,我记得我可以用控制器把字符串放到控制器中并将它转换为HTML - &gt;。

var result = from c in displayedStudents select new[] { Convert.ToString(c.studentnr), c.roepnaam, c.klas, c.adres, c.woonplaats, "<a style='font-family:arial' href='" + "../StudentGegevens/index/" + c.studentnr + "' />Details</a>"};

大声笑,不是我最聪明的人,我很抱歉提出这个问题。