0

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

 <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 之类的方法,但没有运气,它没有返回链接。

4

2 回答 2

1

不太确定要显示什么样的详细数据,但您可以检查方法:fnrender 或 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>';
      } }
             ]
于 2013-03-26T10:39:08.140 回答
0

抱歉,我是个笨蛋,我记得我可以用 HTML 将字符串放入控制器中,然后将其转换为 HTML ->。

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>"};

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

于 2013-03-26T20:27:40.967 回答