【发布时间】:2014-02-22 06:24:18
【问题描述】:
我正在尝试从 asp.net mvc5 中的 ajax 源创建一个 jQuery 数据表。 我想为编辑和删除添加一个额外的列,它不在我的模型类或 ajax 数据源中。对于编辑和删除功能,我需要我的数据表中没有显示的 Id 列的值。
这是我的模型类:
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>';
}
}
]
});
任何帮助将不胜感激。 问候:)
【问题讨论】:
标签: jquery asp.net-mvc jquery-datatables