locked
How can I list all the department related to the depot only in a select List RRS feed

  • Question

  • User-1355965324 posted

    I am using the following model to list  all the depot

    DepotModel
    public int Id
    public string DepotName
    
    Id           DepotName
    1   	Depot 1
    2   	Depot 2
    3   	Depot 3
    4   	Depot 4

     the Department Master Model

    DepartmentModel
    public int DepartmentID
    public string DepartmentName
    
    
    Id	DepartmentName
    1	Floor
    2	Admin
    3	Accounts
    4	Sales
    

    I have another model to give each depot to corresponding Department

    public class DepartmentWorkTime
        {
            [Key]
            public int Id { get; set; }
            public int DepotNo { get; set; }
            public int DepartmentID
           
    
        }
    
    Id     DepotNo      DepartmentID
    1        1                      1
    2        1                      2
    3        2                      1
    4        3                      1
    4        3                      4
    
    
    

     

      List<SelectListItem> depots = new List<SelectListItem>();
                depots = _unitOfWork.Depot.GetAllDepotsForDropdown().ToList();
                depots.RemoveAt(0);
    
                List<SelectListItem> departments = new List<SelectListItem>();
                departments = _unitOfWork.Department.GetAllDepartmentsForDropdown().ToList();

    I  am trying to get here SelectListItem dep be filled with all the department only related to the depot. At the moment it being showed all the department. It doesn't consider the depot Id. for example 

     public class DepartmentMapModel
        {
            public string id { get; set; }
            public string title { get; set; }
        }
    
        public class DepotMapModel
        {
            public string id { get; set; }
            public string title { get; set; }
            public List<DepartmentMapModel> subs { get; set; }
        }
    foreach (SelectListItem dpt in depots)
                {
                    DepotMapModel map = new DepotMapModel();
                    map.id = dpt.Value;
                    map.title = dpt.Text;
                    List<DepartmentMapModel> sub = new List<DepartmentMapModel>();
    // Here I have to check the department is belongs to that depot if so, only should be added sub.Add(depmap)
    foreach (SelectListItem dep in departments) { DepartmentMapModel depmap = new DepartmentMapModel(); depmap.id = dpt.Value + "." + dep.Value; depmap.title = dep.Text; sub.Add(depmap); } map.subs = sub; mapping.Add(map); } return Json(mapping);

    Example 

    If Depot Id =1 Then

    map.id =1 map.Title = Depot 1, map.Subs.Title=Floor map.Subs.Title.Id=1,

    then map.id =2 map.Title = Depot 1, map.Subs.Title=Admin map.Subs.Title.Id=2,

    If Depot Id =2 Then

    map.id =1 map.Title = Depot 2, map.Subs.Title=Floor map.Subs.Title.Id=1,

     Please can you help to get the  SelectList  of the Department for corresponding Depot only

    Friday, July 24, 2020 1:09 PM

Answers

  • User711641945 posted

    Hi polachan,

    For this error,just change like below:

    public JsonResult GetDepotDepartemntsForMap()
    { 
    
        string lson = _unitOfWork.Department.GetDepotWithDepartment();
        return new JsonResult(data);
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2020 7:43 AM

All replies

  • User475983607 posted

    Like many of your posts, you're asking the community to guess how your code works.  We cannot see your Unit-of-Work design and can only assume there are problems with the design.

    If you where using EF the query is remarkably simple.  This assumes your Entities are properly configured which the community also cannot see. The following code is a guess due to the incomplete code; https://docs.microsoft.com/en-us/ef/core/querying/related-data

    var query = dbContext.Depot.Include(m => m.Departments);

    If you want community assistance, I recommend publishing your code on github.  Then community members can submit pull requests -> bug fix recommendations.

    My best recommendation is you take the time to learn programming basics.  If you implement a programming pattern like the Unit-of-Work then you it is up to you learn the pattern and make sure oyu are using it correctly.  It is not the community's responsibility to figure out the holes in your understanding and develop a custom curriculum on these forums.

    Friday, July 24, 2020 2:01 PM
  • User711641945 posted

    Hi polachan,

    From this thread,it seems you want to get the json string from backend like below:

    [
        {
          id: 1,
          title: 'Depot 1',
          subs: [
            {
              id: 1.10,
              title: 'Depot 1.1'
            }, {
              id: 1.11,
              title: 'Depot 1.2'
            }, {
              id: 1.12,
              title: 'Depot 1.3'
            }
          ]
        }
        // more data here
    ]

    If I understand your requirement correctly,you could follow the demo below:

    Model:

    public class DepotModel
    {
        public int Id { get; set; }
        public string DepotName { get; set; }
        public List<DepartmentWorkTime> DepartmentWorkTime { get; set; }
    }
    public class DepartmentModel
    {
        [Key]
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }
        public List<DepartmentWorkTime> DepartmentWorkTime { get; set; }
    }
    public class DepartmentWorkTime
    {
        [Key]
        public int Id { get; set; }
        public int DepotNo { get; set; }
        public DepotModel DepotModel { get; set; }
        public int DepartmentID { get; set; }
        public DepartmentModel DepartmentModel { get; set; }
    }

    ViewModel:

    public class DepartmentMapModel
    {
        public string id { get; set; }
        public string title { get; set; }
    }
    
    public class DepotMapModel
    {
        public string id { get; set; }
        public string title { get; set; }
        public List<DepartmentMapModel> subs { get; set; }
    }

    DbContext:

    public class YourDbContext: DbContext
    {
        public YourDbContext(DbContextOptions<MvcProj3_1Context> options)
            : base(options)
        {
        }
    
        public DbSet<DepotModel> DepotModel { get; set; }
        public DbSet<DepartmentModel> DepartmentModel { get; set; }
        public DbSet<DepartmentWorkTime> DepartmentWorkTime { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
    //configure many-to-many relationship modelBuilder.Entity<DepartmentWorkTime>() .HasKey(bc => new { bc.DepotNo, bc.DepartmentID }); modelBuilder.Entity<DepartmentWorkTime>() .HasOne(bc => bc.DepotModel) .WithMany(b => b.DepartmentWorkTime) .HasForeignKey(bc => bc.DepotNo); modelBuilder.Entity<DepartmentWorkTime>() .HasOne(bc => bc.DepartmentModel) .WithMany(c => c.DepartmentWorkTime) .HasForeignKey(bc => bc.DepartmentID); } }

    Controller:

    public class TestsController : Controller
    {
        private readonly YourDbContext _context;
    
        public TestsController(YourDbContext context)
        {
            _context = context;
        }
    
        public IEnumerable<DepotMapModel> Index()
        {
            var model = _context.DepotModel
                                .Include(d => d.DepartmentWorkTime)
                                .Select(a => new DepotMapModel()
                                {
                                    id = a.Id.ToString(),
                                    title = a.DepotName,
                                    subs = a.DepartmentWorkTime.Select(d=>new DepartmentMapModel()
                                    {  
                                        id= d.DepartmentModel.DepartmentID.ToString(),
                                        title= d.DepartmentModel.DepartmentName
                                    }).ToList()
                                }).ToList();
    
            return model;
        }
    }

    Result:

    Best Regards,

    Rena

    Monday, July 27, 2020 9:20 AM
  • User-1355965324 posted

    Rena

    I got the following Linq to get the data as tree format . But when I pass the data from the function as jsonREsult ' Cannot convert string into mvc.JasonResult. Please can you give a help

    from the repository , the data is coming in proper way of tree list from this linq sql
    public string   GetDepotWithDepartment()
            {
    
                var list = goContext.goDepartmentWorkTime.
                    GroupBy(d => new { d.DepotID, d.Depot.DepotName })
                .Select(g => new
                {
                    id = g.Key.DepotID,
                    title = g.Key.DepotName,
                    subs = g.Select(dd => new
                    {
                        id = dd.DepotID + "." + dd.DepartmentID,
                        title = dd.Depot.DepotNo + "." + dd.Department.DepartmentName
                    })
                });
                string json = JsonConvert.SerializeObject(list, Formatting.Indented);
    }
    
    

    from my controller

    public JsonResult GetDepotDepartemntsForMap()
            { 
    
                string lson = _unitOfWork.Department.GetDepotWithDepartment();
                //  the data is coming correct format
                //how can I pass the data from here to html combo tree
            }

     The output string is coming is coming the given format

    [
      {
        "id": 1,
        "title": "1-Depot1",
        "subs": [
          {
            "id": "1.1",
            "title": "1.Advt"
          },
          {
            "id": "1.2",
            "title": "1.Admin"
          }
        
        ]
      },
      {
        "id": 2,
        "title": "2-Depot2",
        "subs": [
          {
            "id": "2.1",
            "title": "2.Sales"
          },
          {
            "id": "2.2",
            "title": "2.Admin"
          }
          
        ]
    }
    ]

    I want to bring the jsonResult here in Html function

     function FillDepartmentsMappings() {   
        $.ajax({
            type: "GET",
            url: "/User/GetDepotDepartemntsForMap",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {      
                $('#ddlDepotMapping').comboTree({
                    source: data,
                    isMultiple: true,
                    cascadeSelect: true,
                    collapse: true,
                    selected: defaultSelected
                });        
            },
            failure: function (response) {
                console.log(response.responseText);
            },
            error: function (response) {
                console.log(response.responseText);
            }
        });

    Tuesday, July 28, 2020 6:56 AM
  • User711641945 posted

    Hi polachan,

    For this error,just change like below:

    public JsonResult GetDepotDepartemntsForMap()
    { 
    
        string lson = _unitOfWork.Department.GetDepotWithDepartment();
        return new JsonResult(data);
    }

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 28, 2020 7:43 AM