如何使用GUID主键将数据加载到数据表中?
在我的项目中,我已经使用datatable
库来显示数据,但是我有一个问题.实际上,如果我的数据具有int的主键类型.它工作正常,但我有引导"主键.它会显示一些错误.我已经使用了身份进行授权和授权,因为您知道身份已将guid
类型用作主键.当我想从控制器datatable
获取数据时,UserId出现问题,因为UserId是类型"guid
".
in my project, I have used datatable
library for showing data.but I have a problem with it.in fact, if I have data's with the primary key type of int. it works fine but I have "guid" primary key. it will show some error.I have used identity for authorization and authorization as you know identity have used guid
type for the primary key.when I want to fetch data from controller datatable
had a problem with UserId because UserId is the type of "guid
".
这是GetUsers()
控制器的简单示例,我使用了int的userId类型.
this is a simple example of GetUsers()
controller I have used userId type of 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>';
}
}
]
});
});
通过将这一行"function(data,type,full,meta)"放入渲染函数中并使用full参数可能会解决您的问题.
By putting this line 'function(data, type, full, meta)' in your render function and use full parameter might solve your problem.
例如:
$(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>';
}
}
]
});
});
注意::如果以上方法不能解决问题,请参见下面的链接中的注释,您会有所想法.
Note : if above doesn't solve problem ,see the comments in the below link , you will get an idea.