在我的项目中,我使用datatable
库来显示数据。但是我遇到了问题。事实上,如果我的数据是主键类型为int。它工作正常,但我有“guid”主键。它会显示一些错误。我已使用身份进行授权和授权,因为您知道身份已使用guid
类型作为主键。当我想从控制器datatable
获取数据时,UserId出现问题,因为UserId的类型为“guid
”。
这是GetUsers()
控制器的一个简单示例,我使用了userId类型的int。
public ActionResult GetUsers()
{
List<UserViewModel> userView=new List<UserViewModel>();
var userNew=new UserViewModel
{
UserId = 1,
UserName="info.98@gmail.com",
FirstName="alex",
LastName="leyonan",
Gender=true,
Email ="info.2000@gmail.com",
IsActive =false,
PhoneNumber="111111111111",
Address="USA",
};
userView.Add(userNew);
return Json(new { data = userView }, JsonRequestBehavior.AllowGet);
}
=============================================== ===
$(document).ready(function () {
var oTable = $('#example').DataTable({
"ajax": "/Users/GetUsers",
"columns": [
{ "data": "UserName", },
{ "data": "FirstName", },
{ "data": "LastName", },
{ "data": "Gender", },
{ "data": "Email", },
{ "data": "IsActive", },
{ "data": "PhoneNumber", },
{ "data": "Address", },
{
"data": "UserId", "width": "50px", "render": function (data) {
return '<a class="popup" href="/Users/Edit/' + data + '">Edit</a>';
}
},
{
"data": "UserId", "width": "50px", "render": function (data) {
return '<a class="popup" href="/Users/Delete/' + data + '">Delete</a>';
}
}
]
});
});
答案 0 :(得分:0)
UserId必须是字符串类型。
public ActionResult GetUsers()
{
List<UserViewModel> userView=new List<UserViewModel>();
var userNew=new UserViewModel
{
UserId = Guid.NewGuid().ToString(), //Convert into string
UserName="info.98@gmail.com",
FirstName="alex",
LastName="leyonan",
Gender=true,
Email ="info.2000@gmail.com",
IsActive =false,
PhoneNumber="111111111111",
Address="USA",
};
userView.Add(userNew);
return Json(new { data = userView }, JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:0)
通过设置此行&#39;功能(数据,类型,完整,元)&#39;在你的渲染功能和使用完整参数可能会解决你的问题。
<强>实施例强>
$(document).ready(function () {
var oTable = $('#example').DataTable({
"ajax": "/Users/GetUsers",
"columns": [
{ "data": "UserName", },
{ "data": "FirstName", },
{ "data": "LastName", },
{ "data": "Gender", },
{ "data": "Email", },
{ "data": "IsActive", },
{ "data": "PhoneNumber", },
{ "data": "Address", },
{
"data": "UserId", "width": "50px", "render":
//change
function(data, type, full, meta) {
return '<a class="popup" href="/Users/Edit/' + data + '">Edit</a>';
}
},
{
"data": "UserId", "width": "50px", "render":
//change
function(data, type, full, meta) {
return '<a class="popup" href="/Users/Delete/' + data + '">Delete</a>';
}
}
]
});
});
注意:如果上述问题无法解决问题,请参阅以下链接中的评论,您将会有所了解。
有用的链接: How to set data for a specific column using jQuery DataTables table plugin in ASP.NET MVC?