Home how to merge two array list in JSON and view in data table rows using asp.net MVC?
Reply: 1

how to merge two array list in JSON and view in data table rows using asp.net MVC?

Ivin Raj
1#
Ivin Raj Published in 2017-12-07 12:26:38Z

text and EmpList having two arraylist how to return two array list in JSON and how to view in data table rows in a single view .i have split the code two $.each(data.EmpList, function (i, item) and $.each(data.test, function (i, item) condition how to merge both array values in a single Row view..

$("#DomainID").change(function () {

                var id = $(this).val();
                $("#example tbody tr").remove();

                $.ajax({

                    type: 'POST',

                    url: '@Url.Action("ViewModules")',
                    dataType: 'json',
                    data: { id: id },
                    success: function (data) {
                        var items = '';
                        $.each(data.EmpList, function (i, item) {
                            $("#findValue").show();
                            var rows = "<tr>"

                            + "<td>" + item.ModuleName + "</td>"
                            + "<td>" + item.Url + "</td>"
                            + "<td>" + item.RoleName  + "</td>"
                            + "</tr>";
                            $('#example tbody').append(rows);
                        });

                        $.each(data.test, function (i, item) {
                            $("#findValue").show();
                            var rows = "<tr>"
                            + "<td>" + item.RoleName + "</td>"
                            + "</tr>";
                            $('#example tbody').append(rows);
                        });

                    },
                    error: function (ex) {
                        var r = jQuery.parseJSON(response.responseText);
                        alert("Message: " + r.Message);
                        alert("StackTrace: " + r.StackTrace);
                        alert("ExceptionType: " + r.ExceptionType);
                    }
                });
                return false;
            })

Controller:

public ActionResult ViewModules(int id)
        {
            Domain_Bind();
            dynamic mymodel = new ExpandoObject();
            userType type = new userType();
            List<ViewRoleModules> EmpList = type.GetRoleModulesViews(id);
            string sRptModuleIDs = string.Empty;
            foreach (ViewRoleModules emp in EmpList)
            {
                sRptModuleIDs += emp.ModuleID + ",";
            }
            if (sRptModuleIDs != "")
            {
                sRptModuleIDs = sRptModuleIDs.Remove(sRptModuleIDs.Length - 1, 1);
            }

            List<ViewRoleModules> RoleList;
            List<ViewRoleModules> test = new List<ViewRoleModules>();
            foreach (var rid in sRptModuleIDs.Split(','))
            {
                string RID = rid;
                RoleList = type.GetSiteRoleModulesViews(rid);
                foreach (ViewRoleModules vip in RoleList)
                {
                    test.Add(new ViewRoleModules {
                        RoleName = vip.RoleName
                    });
                }
            }

            var data = new { EmpList = EmpList, test = test };

            return Json(data, JsonRequestBehavior.AllowGet);

        }

Here my ouptput screen:

public class ViewRoleModules
    {
        [Key]
        public int ModuleID { get; set; }
        public int DomainID { get; set; }
        public int ParentModuleID { get; set; }
        public int CompanyTypeID { get; set; }
        public string ModuleName { get; set; }
        public string FolderName { get; set; }
        public string Url { get; set; }
        public int TabOrder { get; set; }
        public string Style { get; set; }
        public string Status { get; set; }
        public string IsTab { get; set; }
        public string ApprovalProcess { get; set; }
        public int CreatedBy { get; set; }
        public DateTime CreatedDate = DateTime.UtcNow;
        public int ModifiedBy { get; set; }
        public DateTime ModifiedDate = DateTime.UtcNow;

        public string RoleName { get; set; }
        public int RoleID { get; set; }

    }

And

public List<ViewRoleModules> GetRoleModulesViews(int id)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Admin"].ConnectionString))
            {
                List<ViewRoleModules> EmpList = new List<ViewRoleModules>();
                SqlCommand com = new SqlCommand("MEDEIL_Modules_SelectDomainModules", conn);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@DomainID", id);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();

                conn.Open();
                da.Fill(dt);
                conn.Close();
                foreach (DataRow dr in dt.Rows)
                {

                    EmpList.Add(

                        new ViewRoleModules
                        {
                            ModuleID = Convert.ToInt32(dr["ModuleID"]),
                            CompanyTypeID = Convert.ToInt32(dr["CompanyTypeID"]),
                            DomainID = Convert.ToInt32(dr["DomainID"]),
                            ParentModuleID = Convert.ToInt32(dr["ParentModuleID"]),
                            ModuleName = Convert.ToString(dr["ModuleName"]),
                            FolderName = Convert.ToString(dr["FolderName"] == DBNull.Value ? null : dr["FolderName"].ToString()),
                            Url = Convert.ToString(dr["Url"]),
                            TabOrder = Convert.ToInt32(dr["TabOrder"]),
                            Style = Convert.ToString(dr["Style"]),
                            Status = Convert.ToString(dr["Status"]),
                            IsTab = Convert.ToString(dr["IsTab"]),
                            ApprovalProcess = Convert.ToString(dr["ApprovalProcess"]),
                            CreatedBy = Convert.ToInt32(dr["CreatedBy"] == DBNull.Value ? null : dr["CreatedBy"].ToString()),
                            CreatedDate = Convert.ToDateTime(dr["CreatedDate"]),
                            ModifiedBy = Convert.ToInt32(dr["ModifiedBy"] == DBNull.Value ? null : dr["ModifiedBy"].ToString()),
                            ModifiedDate = Convert.ToDateTime(dr["ModifiedDate"] == DBNull.Value ? null : dr["ModifiedDate"].ToString())
                        }
                    );
                }

                return EmpList;
            }
        }


        public List<ViewRoleModules> GetSiteRoleModulesViews(string rid)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Admin"].ConnectionString))
            {
                List<ViewRoleModules> RoleList = new List<ViewRoleModules>();
                SqlCommand com = new SqlCommand("MEDEIL_SiteRoleModules_SelectOne", conn);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ModuleID", Convert.ToInt32(rid));
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();

                conn.Open();
                da.Fill(dt);
                conn.Close();
                foreach (DataRow dr in dt.Rows)
                {

                    RoleList.Add(

                        new ViewRoleModules
                        {
                            //RoleID = Convert.ToInt32(dr["RoleID"]),
                            RoleName = Convert.ToString(dr["RoleName"])

                        }
                    );
                }

                return RoleList;
            }
        }
Kumar_Vikas
2#
Kumar_Vikas Reply to 2017-12-08 05:05:39Z

Current scenario that you are working on, where each module can have multiple roles.EmpList and test are returned as result. Here I have used ModuleId as a common value for linking two lists.

C#:

Controller:

public ActionResult ViewModules(int id)
        {
            Domain_Bind();
            dynamic mymodel = new ExpandoObject();
            userType type = new userType();


            List<ViewRoleModules> EmpList = type.GetRoleModulesViews(id);

            List<ViewRoleModules> RoleList;
            List<ViewRoleModules> test = new List<ViewRoleModules>();
            foreach (ViewRoleModules emp in EmpList)
            {
                RoleList = type.GetSiteRoleModulesViews(emp.ModuleId);
                foreach (ViewRoleModules vip in RoleList)
                {
                    test.Add(new ViewRoleModules
                    {
                        RoleName = vip.RoleName,
                        ModuleId = vip.ModuleId
                    });
                }
            }

            var data = new { EmpList = EmpList, test = test };

            return Json(data, JsonRequestBehavior.AllowGet);

        }

Change made to class GetSiteRoleModulesViews, added ModuleId.

public List<ViewRoleModules> GetSiteRoleModulesViews(int _ModuleId)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Admin"].ConnectionString))
            {
                List<ViewRoleModules> RoleList = new List<ViewRoleModules>();
                SqlCommand com = new SqlCommand("MEDEIL_SiteRoleModules_SelectOne", conn);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ModuleID", _ModuleId);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();

                conn.Open();
                da.Fill(dt);
                conn.Close();
                foreach (DataRow dr in dt.Rows)
                {

                    RoleList.Add(

                        new ViewRoleModules
                        {
                            //RoleID = Convert.ToInt32(dr["RoleID"]),
                            RoleName = Convert.ToString(dr["RoleName"])
                            //*****Included ModuleID Here*****
                            ModuleId = _ModuleId;
                        }
                    );
                }

                return RoleList;
            }
        }

JS:

$("#DomainID").change(function () {

                var id = $(this).val();
                $("#example tbody tr").remove();

                $.ajax({

                    type: 'POST',

                    url: '@Url.Action("ViewModules")',
                    dataType: 'json',
                    data: { id: id },
                    success: function (data) {
                        var items = '';
                        $.each(data.EmpList, function (i, item) {
                            $("#findValue").show();

                            /*Find Role here - Comparing Emp List ModuleId to RoleList ModuleId*/

                            var Result = $(data.test).filter(function (index, element) {
                                return element.ModuleId == item.ModuleId
                            });

                            var rows = "<tr>"
                            + "<td>" + item.ModuleName + "</td>"
                            + "<td>" + item.Url + "</td>"
                            + "<td>" + Result.RoleName + "</td>"
                            + "</tr>";
                            $('#example tbody').append(rows);
                        });

                    },
                    error: function (ex) {
                        var r = jQuery.parseJSON(response.responseText);
                        alert("Message: " + r.Message);
                        alert("StackTrace: " + r.StackTrace);
                        alert("ExceptionType: " + r.ExceptionType);
                    }
                });
                return false;
    });
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.343395 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO