Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从jQuery datatable中的ajax数据源获取mRender()函数中的隐藏列值_Jquery_Asp.net Mvc_Jquery Datatables - Fatal编程技术网

如何从jQuery datatable中的ajax数据源获取mRender()函数中的隐藏列值

如何从jQuery datatable中的ajax数据源获取mRender()函数中的隐藏列值,jquery,asp.net-mvc,jquery-datatables,Jquery,Asp.net Mvc,Jquery Datatables,我正在尝试从asp.NETMVC5中的ajax源创建一个jQuery数据表。 我想为edit and delete添加一个额外的列,该列不在我的模型类或ajax数据源中。对于edit and delete功能,我需要Id列的值,该值未显示在我的datatable中 这是我的模型课: public class Country { public int Id { get; set; } [Required(ErrorMessage = "Country Code Name Must

我正在尝试从asp.NETMVC5中的ajax源创建一个jQuery数据表。 我想为edit and delete添加一个额外的列,该列不在我的模型类或ajax数据源中。对于edit and delete功能,我需要Id列的值,该值未显示在我的datatable中

这是我的模型课:

public class Country
{
    public int Id { get; set; }
    [Required(ErrorMessage = "Country Code Name Must not be empty")]
    public String Code { get; set; }
    [Required(ErrorMessage = "Country Name Must not be empty")]
    public String Name { get; set; }
    [Required(ErrorMessage = "Template Name Must not be empty")]
    public String Template { get; set; }
    [Required(ErrorMessage = "SPViews Name Must not be empty")]
    public String SPViews { get; set; }
}
这是我的控制器:

    public ActionResult GetAll(JQueryDataTableParamModel param)
    {
        var countryList = _repository.GetAll().ToList();
        var filteredCountry = (from e in countryList
                               where (param.sSearch == null || e.Name.ToLower().Contains(param.sSearch.ToLower()))
                               select e).ToList();
        var result = from country in filteredCountry.Skip(param.iDisplayStart).Take(param.iDisplayLength)
                     select new[] { country.Id,country.Code, country.Name, country.Template, country.SPViews };

        return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = countryList.Count(),
            iTotalDisplayRecords = filteredCountry.Count,
            aaData = result
        }, JsonRequestBehavior.AllowGet);
    }
这是我的html表格:

<table id="countryListTable" class="table table-condensed">
<thead>
    <tr>
        <th>Id</th>
        <th>Code</th>
        <th>Name</th>
        <th>Template</th>
        <th>SPViews</th>
        <th>&nbsp;</th>
    </tr>
</thead>
<tbody>
</tbody>
</table>

身份证件
代码
名称
模板
SPViews
最后是我的jQuery代码:

     var countryTable = $("#countryListTable").dataTable({
            "bServerSide": true,
            "bProcessing": true,
            "sAjaxSource": "/Country/GetAll",
            "aoColumns": [
                null,
                null,
                null,
                null,
                {     // fifth column (Edit link)
                    "mData": "Id",
                    "bSearchable": false,
                    "bSortable": false,
                    "mRender": function (nRow, aData) {
                        //need to get the Id column value
                        return '<a class="glyphicon glyphicon-pencil" href="/Country/Edit/">Edit</a><a class="glyphicon glyphicon-remove" href="/Country/Delete/">Delete</a>';
                    }
                }
            ]
        });
var countryTable=$(“#countryListTable”).dataTable({
“bServerSide”:正确,
“bProcessing”:正确,
“sAjaxSource”:“/Country/GetAll”,
“aoColumns”:[
无效的
无效的
无效的
无效的
{//第五列(编辑链接)
“mData”:“Id”,
“可搜索”:错误,
“可移植”:错误,
“mRender”:功能(nRow,aData){
//需要获取Id列的值
返回“”;
}
}
]
});
任何帮助都将不胜感激。
注意:)

首先,我会尝试使用
aoColumnDefs
而不是
aoColumns

根据datatables文档:

aoColumnDefs:此数组允许您以特定列为目标, 多列或所有列,使用每个列的aTargets属性 对象(请注意,aoColumnDefs是在 数据表1.7)。这使得创建表时具有极大的灵活性, 因为aoColumnDefs数组可以是任意长度的,以列为目标 你特别想要的

接下来,我不太清楚您打算如何在编辑和删除链接中使用
Id
, 但是在这里,
Id
被附加到
url

  "aoColumnDefs": [
        { "mData": "Code ", "aTargets": [ 0 ] },
        { "mData": "Name", "aTargets": [ 1 ] },
        { "mData": "Template", "aTargets": [ 2 ] },
        { "mData": "SPViews", "aTargets": [ 3 ] },               
        { "mData": "Id", "aTargets": [ 4 ], 
            "mRender": function ( data, type, full ) {
                return '<a class="glyphicon glyphicon-pencil" href="/Country/Edit/' + data + '">Edit</a><a class="glyphicon glyphicon-remove" href="/Country/Delete/' + data + '">Delete</a>';
            }
        },
        { "bSortable": false, "aTargets": [ 4 ] }
    ],

@mg1075谢谢您的回复fnRender函数似乎是。我没有尝试您的解决方案,但我使用mRender函数以另一种方式修复了它。 因此,我的解决方案如下:

      countryTable = $("#countryListTable").dataTable({
            "bServerSide": true,
            "bProcessing": true,
            "sAjaxSource": "/Country/GetAll",
            "aoColumns": [
                 { "bVisible": false },
                  null,
                  null,
                  null,
                  null,
                  {
                      mData: 0,//The Id column
                      "bSearchable": false,
                      "bSortable": false,
                      mRender: function (data, type, row) {

                          return '<a class="glyphicon glyphicon-pencil" onclick="editCountry(\'/Country/Edit/' + data + '\');return false;">Edit</a><a class="glyphicon glyphicon-remove" onclick="deleteCountry(\'/Country/Delete/' + data + '\');return false;">Delete</a>';
                      }
                  }],

        });
countryTable=$(“#countryListTable”).dataTable({
“bServerSide”:正确,
“bProcessing”:正确,
“sAjaxSource”:“/Country/GetAll”,
“aoColumns”:[
{“bVisible”:false},
无效的
无效的
无效的
无效的
{
mData:0,//Id列
“可搜索”:错误,
“可移植”:错误,
mRender:函数(数据、类型、行){
返回'EditDelete';
}
}],
});
我认为这两种方法都应该是完美的

      countryTable = $("#countryListTable").dataTable({
            "bServerSide": true,
            "bProcessing": true,
            "sAjaxSource": "/Country/GetAll",
            "aoColumns": [
                 { "bVisible": false },
                  null,
                  null,
                  null,
                  null,
                  {
                      mData: 0,//The Id column
                      "bSearchable": false,
                      "bSortable": false,
                      mRender: function (data, type, row) {

                          return '<a class="glyphicon glyphicon-pencil" onclick="editCountry(\'/Country/Edit/' + data + '\');return false;">Edit</a><a class="glyphicon glyphicon-remove" onclick="deleteCountry(\'/Country/Delete/' + data + '\');return false;">Delete</a>';
                      }
                  }],

        });