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
- public class Tests {
- [Key]
- public int Id { get; set; }
- [Required] [Display(Name = "Test Name")]
- public string TestName { get; set; }
- [Display(Name = "Short Name")]
- public string ShortName { get; set; }
- [Display(Name="Technical Name")]
- public string TechName { get; set; }
- [Required] [Display(Name ="Test Price")]
- public float TestPrice { get; set; }
- [Display(Name = "Sub Department")]
- public int SubDeptId { get; set; }
- [Display(Name = "Center")]
- public int CenterId { get; set; }
- public string Separate { get; set; }
- [Display(Name = "Sub Department")]
- [ForeignKey("SubDeptId")]
- public virtual SubDepartments subDepartments { get; set; }
- [Display(Name = "Centers")] [ForeignKey("CenterId")]
- public virtual Centers centers { get; set; } }
Model TestParameter
- public class TestParameter {
- [Key]
- public int Id { get; set; }
- [Required]
- public string Categories { get; set; }
- [Required]
- [Display(Name = "Test Parameter Name")]
- public string ParameterName { get; set; }
- [Required] public string Unit { get; set; }
- [Display(Name ="Decimal Point")]
- public int DecimalPoint { get; set; }
- [Display(Name = "Help Value")]
- public string HelpValue { get; set; }
- [Display(Name = "Normal Range")]
- public string NormalRange { get; set; }
- public string Minimum { get; set; }
- public string Maximum { get; set; }
- [Display(Name="Test Footer")]
- public string TestFooter { get; set; }
- [Display(Name = "Tests Name")]
- public int TestId { get; set; }
- [ForeignKey("TestId")]
- public virtual Tests Tests { get; set; }
- }
Model PatientTest
- public class PatientTest {
- [Key]
- public int Id { get; set; }
- [Display(Name ="Patient Id")]
- public int PatientId { get; set; }
- [Display(Name ="Test Id")]
- public int TestId { get; set; }
- [Display(Name ="Doctor")]
- public int DoctorId { get; set; }
- [Display(Name="Center")]
- public int CenterId { get; set; }
- [Display(Name = "Test")]
- [ForeignKey("TestId")]
- public virtual Tests Tests { get; set; }
- [Display(Name = "Doctor Reference")]
- [ForeignKey("DoctorId")]
- public virtual Doctors Doctors { get; set; }
- [Display(Name = "Center Reference")]
- [ForeignKey("CenterId")]
- public virtual Centers Centers { get; set; }
- [Display(Name = "Patient")]
- [ForeignKey("PatientId")]
- public virtual Patient Patient { get; set; }
- }
PatientTests contains relations of patient, tests, testparameters.
the controller is PatientTestsController given here
- public async Task<IActionResult> TestResult([FromForm]Patient_Tests_TestParameter pt) {
- if (pt.patient.VisitNo == null)
- { return NotFound(); }
- Patient_Tests_TestParameter pttp = new Patient_Tests_TestParameter();
- var visitno = pt.patient.VisitNo;
- pttp.patient = _db.Patient.Single(p=>p.VisitNo==visitno);
- var patientTests = _db.PatientTests
- .Include(x => x.Tests)
- .Where(x => x.PatientId == pttp.patient.Id)
- .ToList();
- var testIds = patientTests.Select(x => x.TestId).ToList();
- var testParameters = _db.TestParameters
- .Where(x => testIds.Contains(x.TestId))
- .ToList();
- ViewData["test"] = patientTests;
- ViewData["testPara"] = testParameters;
- return View(pttp);
- }
the data has to be present by ViewData["test"] and ViewData["testPara"] ViewData["test"] is parent of ViewData["testPara"]