how to load data into datatable that using GUID primary key?

限于喜欢 提交于 2020-01-16 08:03:56

问题


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".

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>';
                    }
                }
            ]
        });
    });

回答1:


By putting this line 'function(data, type, full, meta)' in your render function and use full parameter might solve your problem.

Ex:

    $(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.

Useful Link:How to set data for a specific column using jQuery DataTables table plugin in ASP.NET MVC?




回答2:


UserId must be as string type.

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);

    }


来源:https://stackoverflow.com/questions/45163155/how-to-load-data-into-datatable-that-using-guid-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!