Sk Jha

Sk Jha

  • NA
  • 113
  • 43.3k

re-arange data with parent-child relation in asp.net-core

Apr 6 2020 10:36 PM
I need to re-arange data with parent-child relation Tests is parent and TestParameter is child of Tests.
present data Like this hierarchal format.
 
Test1
   TestPara1
   TestPara2
   TestPara3
 
Test2
   TestPara1
   TestPara2
   TestPara3
 
the data has to be present in table in this given format.
 
The models are given here:
 
Model Tests
  1. public class Tests {  
  2. [Key]  
  3. public int Id { getset; }  
  4. [Required] [Display(Name = "Test Name")]  
  5. public string TestName { getset; }  
  6. [Display(Name = "Short Name")]  
  7. public string ShortName { getset; }  
  8. [Display(Name="Technical Name")]  
  9. public string TechName { getset; }  
  10. [Required] [Display(Name ="Test Price")]  
  11. public float TestPrice { getset; }  
  12. [Display(Name = "Sub Department")]  
  13. public int SubDeptId { getset; }  
  14. [Display(Name = "Center")]  
  15. public int CenterId { getset; }  
  16. public string Separate { getset; }  
  17. [Display(Name = "Sub Department")]  
  18. [ForeignKey("SubDeptId")] //relation of departments table  
  19. public virtual SubDepartments subDepartments { getset; }  
  20. [Display(Name = "Centers")] [ForeignKey("CenterId")] //relation of departments table  
  21. public virtual Centers centers { getset; } }  
Model TestParameter
  1. public class TestParameter {  
  2. [Key]  
  3. public int Id { getset; }  
  4. [Required]  
  5. public string Categories { getset; }  
  6. [Required]  
  7. [Display(Name = "Test Parameter Name")]  
  8. public string ParameterName { getset; }  
  9. [Required] public string Unit { getset; }  
  10. [Display(Name ="Decimal Point")]  
  11. public int DecimalPoint { getset; }  
  12. [Display(Name = "Help Value")]  
  13. public string HelpValue { getset; }  
  14. [Display(Name = "Normal Range")]  
  15. public string NormalRange { getset; }  
  16. public string Minimum { getset; }  
  17. public string Maximum { getset; }  
  18. [Display(Name="Test Footer")]  
  19. public string TestFooter { getset; }  
  20. [Display(Name = "Tests Name")]  
  21. public int TestId { getset; }  
  22. [ForeignKey("TestId")] //relation of departments table  
  23. public virtual Tests Tests { getset; }  
  24. }  
Model PatientTest
  1. public class PatientTest {  
  2. [Key]  
  3. public int Id { getset; }  
  4. [Display(Name ="Patient Id")]  
  5. public int PatientId { getset; }  
  6. [Display(Name ="Test Id")]  
  7. public int TestId { getset; }  
  8. [Display(Name ="Doctor")]  
  9. public int DoctorId { getset; }  
  10. [Display(Name="Center")]  
  11. public int CenterId { getset; }  
  12. [Display(Name = "Test")]  
  13. [ForeignKey("TestId")] //relation of Tests table  
  14. public virtual Tests Tests { getset; }  
  15. [Display(Name = "Doctor Reference")]  
  16. [ForeignKey("DoctorId")] //relation of Doctors table  
  17. public virtual Doctors Doctors { getset; }  
  18. [Display(Name = "Center Reference")]  
  19. [ForeignKey("CenterId")] //relation of Centers table  
  20. public virtual Centers Centers { getset; }  
  21. [Display(Name = "Patient")]  
  22. [ForeignKey("PatientId")] //relation of Patient table  
  23. public virtual Patient Patient { getset; }  
  24. }  
PatientTests contains relations of patient, tests, testparameters.
 
the controller is PatientTestsController given here
  1. public async Task<IActionResult> TestResult([FromForm]Patient_Tests_TestParameter pt) {  
  2. if (pt.patient.VisitNo == null)  
  3. return NotFound(); }  
  4. Patient_Tests_TestParameter pttp = new Patient_Tests_TestParameter();  
  5. var visitno = pt.patient.VisitNo;  
  6. pttp.patient = _db.Patient.Single(p=>p.VisitNo==visitno);  
  7. var patientTests = _db.PatientTests  
  8. .Include(x => x.Tests)  
  9. .Where(x => x.PatientId == pttp.patient.Id)  
  10. .ToList();  
  11. var testIds = patientTests.Select(x => x.TestId).ToList();  
  12. var testParameters = _db.TestParameters  
  13. .Where(x => testIds.Contains(x.TestId))  
  14. .ToList();  
  15. ViewData["test"] = patientTests;  
  16. ViewData["testPara"] = testParameters;  
  17. return View(pttp);  
  18. }  
the data has to be present by ViewData["test"] and ViewData["testPara"] ViewData["test"] is parent of ViewData["testPara"]