我想從asp.net mvc5中的ajax源創建一個jQuery數據表。 我想添加一個額外的列進行編輯和刪除,這不在我的模型類或ajax數據源中。對於編輯和刪除功能,我需要Id列的值,我沒有在我的數據表中顯示。如何從ajax數據源在jQuery數據表中獲取mRender()函數中的隱藏列值
這裏是我的模型類:
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>';
}
}
]
});
任何幫助,將不勝感激。 問候:)