如何将数据加载到使用 GUID 主键的数据表中?
how to load data into datatable that using GUID primary key?
在我的项目中,我使用 datatable
库来显示 data.but 我有一个问题 it.in 事实上,如果我的数据的主键类型为 int。它工作正常,但我有 "guid" 主键。它将显示一些 error.I 已使用身份进行授权和授权,因为你知道身份已使用 guid
主要类型 key.when 我想从控制器获取数据 datatable
有一个UserId 的问题,因为 UserId 是“guid
”的类型。
这是一个简单的例子 GetUsers()
controller 我使用了 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>';
}
}
]
});
});
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);
}
将此行 'function(data, type, full, meta)' 放入您的渲染函数并使用完整参数可能会解决您的问题。
例如:
$(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>';
}
}
]
});
});
注意:如果上面没有解决问题,请看下面的评论link,你会明白的。
有用Link:How to set data for a specific column using jQuery DataTables table plugin in ASP.NET MVC?
在我的项目中,我使用 datatable
库来显示 data.but 我有一个问题 it.in 事实上,如果我的数据的主键类型为 int。它工作正常,但我有 "guid" 主键。它将显示一些 error.I 已使用身份进行授权和授权,因为你知道身份已使用 guid
主要类型 key.when 我想从控制器获取数据 datatable
有一个UserId 的问题,因为 UserId 是“guid
”的类型。
这是一个简单的例子 GetUsers()
controller 我使用了 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>';
}
}
]
});
});
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);
}
将此行 'function(data, type, full, meta)' 放入您的渲染函数并使用完整参数可能会解决您的问题。
例如:
$(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>';
}
}
]
});
});
注意:如果上面没有解决问题,请看下面的评论link,你会明白的。
有用Link:How to set data for a specific column using jQuery DataTables table plugin in ASP.NET MVC?