0
次のコードを使用してデータを入力し、AjaxでDataTableを入力します。MVCはjsonをDataTableにエンコードします
私の問題は、私がデータベースに会社名を保存するときにAllowHtml(必要)を使用することです。
だから私の質問は: どのように私は= asset.CompanyNameので、データテーブルいけないタイトルは写真のようなスクリプト/ HTMLを取得しエンコードするのですか?
// GET: Jsons/Customers
public JsonResult Customers([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
{
Db db = new Db();
IQueryable<CustomersDTO> query = db.Customers.Where(x => x.CompanyId == companyId);
var totalCount = query.Count();
#region Filtering
// Apply filters for searching
if (requestModel.Search.Value != string.Empty)
{
var value = requestModel.Search.Value.Trim();
query = query.Where(p => p.Id.ToString().Contains(value.ToString()) ||
p.CompanyName.Contains(value)
);
}
var filteredCount = query.Count();
#endregion Filtering
#region Sorting
// Sorting
var sortedColumns = requestModel.Columns.GetSortedColumns();
var orderByString = String.Empty;
foreach (var column in sortedColumns)
{
orderByString += orderByString != String.Empty ? "," : "";
orderByString += (column.Data) + (column.SortDirection == Column.OrderDirection.Ascendant ? " asc" : " desc");
}
query = query.OrderBy(orderByString == string.Empty ? "Id asc" : orderByString);
#endregion Sorting
// Paging
query = query.Skip(requestModel.Start).Take(requestModel.Length);
var data = query.Select(asset => new
{
Id = asset.Id,
//Allowing HTML for CompanyName
Title = asset.CompanyName,
Zip = asset.Zip,
City = asset.City,
Active = asset.Active
}).ToList();
return Json(new DataTablesResponse(requestModel.Draw,data, filteredCount, totalCount), JsonRequestBehavior.AllowGet);
}
ページのコード:
var assetListVM;
$(function () {
assetListVM =
{
dt: null,
init: function ()
{
dt = $('#assets-data-table').DataTable(
{
"language":
{
"url": "/Scripts/plugins/dataTables/Swedish.json"
},
"serverSide": true,
"processing": true,
"ajax":
{
"url": "@Url.Action("Customers", "Jsons")",
"data": function (d)
{
d.parameter1 = "Id";
d.parameter2 = "Title";
}
},
"columns":
[
{ "title": "Id", "data": "Id", "searchable": true },
{
"title": "Rubrik",
"searchable": true,
"data": null,
"className": "class1 class2",
"orderable": false,
"render": function (data, type, row) {
var someUrl = "/Admin/ShowCustomer/" + data.Id;
return '<a href="' + someUrl + '" class="openEditor">' + data.Title + '</a>';
}
},
{ "title": "Postnr", "data": "Zip", "searchable": true },
{ "title": "Stad", "data": "City", "searchable": true },
{ "title": "Aktiv", "data": "Active", "searchable": true }
],
"lengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
});
}
}
// initialize the datatables
assetListVM.init();
});
コードで私の投稿を編集しました – Katey