我试图从asp.net mvc5中的ajax源创建jQuery数据表。我想添加一个额外的列用于编辑和删除,该列不在我的模型类或ajax数据源中。对于编辑和删除功能,我需要Id列的值,该值未在数据表中显示。
这是我的Model类:
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> </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
最后是我的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>';
}
}
]
});
任何帮助,将不胜感激。问候 :)
@ 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>';
}
}],
});
我认为两种方法都应该是完美的
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句