如何使用GUID主键将数据加载到数据表中? [英] how to load data into datatable that using GUID primary key?

查看:136
本文介绍了如何使用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.

有用链接: 如何使用ASP.NET中的jQuery DataTables表插件为特定列设置数据MVC?

这篇关于如何使用GUID主键将数据加载到数据表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆