Nested Table with expand collapse in ASP.Net MVC

Last Reply 6 months ago By alibasha

Posted 6 months ago

Hi,

I am new to MVC. I need to do a task, where I need to implement Praent-Child table in MVC. Like fetch
data from DB and display Companies(Company Name, Location, TypeOFBusiness) in a table, if we click on CompanyID,
we need to display Employees of that Company as a Child Table(Like EmployeeName,Designation, Location). A
Any references or small POC would be highly appreciated. Thanks.


Posted 6 months ago

Hi,

Thanks for the help. The following link was very helpful to me in doing my task. They have used EF, but I am getting data from WebAPI, I have changed code little bit. For Reference I am adding my code here.

http://www.dotnetawesome.com/2014/07/nested-webgrid-with-expand-collapse-in-aspnet-mvc4.html

 [Route("~/EnquiryDetails/Index")]
        [Authentication]
        public ActionResult EnquiryDetails()
        {
            //All Enquiry Details
            List<EnquiryAllModelBO> objEnEnquiryAllModelBOList = null;
            EnquiryAllModelBO objEnquiryAllModelBO = null;

            List<ParentEnquiryModelBO> objParentEnquiryModelBOList = GetParentEnquiryDetails();

            for (int i = 0; i < objParentEnquiryModelBOList.Count; i++)
            {
                objEnEnquiryAllModelBOList= new List<EnquiryAllModelBO>();
                objEnquiryAllModelBO=new EnquiryAllModelBO();
                objEnquiryAllModelBO.ParentEnquiryList = objParentEnquiryModelBOList[i];
                objEnquiryAllModelBO.ChildEnquiryList = GetChildEnquiryDetails(objParentEnquiryModelBOList[i].ProductEnquiryID);
                objEnEnquiryAllModelBOList.Add(objEnquiryAllModelBO);
            }
            return View(objEnEnquiryAllModelBOList);

        }

 

  private List<ParentEnquiryModelBO> GetParentEnquiryDetails()
        {
            List<ParentEnquiryModelBO> objParentEnquiryModelBOList = new List<ParentEnquiryModelBO>();

            string stringData = string.Empty;
            //ViewBag.UserName = _Username;
            using (var client = new HttpClient())
            {
                //Calling WebAPI, later need to read from Web.Config
                response = client.GetAsync("http://localhost:64297/Admin/Enquiry/GetParentEnquiry/").Result;
                //response = client.GetAsync(_ServiceURL_Admin_Get + "/" + _Username + "/" + SessionManagement.Current.CountryID).Result;

                if (response.IsSuccessStatusCode)
                {
                    stringData = response.Content.ReadAsStringAsync().Result;

                    objParentEnquiryModelBOList = JsonConvert.DeserializeObject<List<ParentEnquiryModelBO>>(stringData);

                }
                else //web api sent error response 
                {
                    //log response status here..                
                    ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
                }
            }

            return objParentEnquiryModelBOList;
        }

        private List<ChildEnquiryModelBO> GetChildEnquiryDetails(int ProductEnquiryID)
        {
            List<ChildEnquiryModelBO> objChildEnquiryModelBOList = new List<ChildEnquiryModelBO>();

            string stringData = string.Empty;
            //ViewBag.UserName = _Username;
            using (var client = new HttpClient())
            {
                //Calling WebAPI, later need to read from Web.Config
                response = client.GetAsync("http://localhost:64297/Admin/Enquiry/GetChildEnquiry/"+ ProductEnquiryID).Result;
                //response = client.GetAsync(_ServiceURL_Admin_Get + "/" + _Username + "/" + SessionManagement.Current.CountryID).Result;

                if (response.IsSuccessStatusCode)
                {
                    stringData = response.Content.ReadAsStringAsync().Result;

                    objChildEnquiryModelBOList = JsonConvert.DeserializeObject<List<ChildEnquiryModelBO>>(stringData);

                }
                else //web api sent error response 
                {
                    //log response status here..                
                    ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
                }
            }

            return objChildEnquiryModelBOList;
        }

 

  public class EnquiryAllModelBO
    {
        public ParentEnquiryModelBO ParentEnquiryList { get; set; }
        public List<ChildEnquiryModelBO> ChildEnquiryList { get; set; }
    }

public class ParentEnquiryModelBO
    {
        public int ProductEnquiryID { get; set; }
        public DateTime EnquiryDate { get; set; }
        public string CustomerName { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }

}

 public class ChildEnquiryModelBO
    {
        public int ProductLineID { get; set; }
        public int ProductEnquiryID { get; set; }
        public int InventoryConfigID { get; set; }
        //public string ProductName { get; set; }
        public int Qty { get; set; }

}