Madhav

Madhav

  • NA
  • 196
  • 43.3k

How to pass multiple models to view in MVC in .net core 2.0

Jun 4 2018 4:35 AM
Here i want to pass data from multiple tables to MVC view in .net core 2.0.
 
here i have some ms sql tables which are dependant,
 
these are like below.
 
1. Organizations
2. Primary Contact Information
3. Regions.
4. Renewals.
 
1.Orgnization table have Organization ID(PK), Name.. and Region ID(FK from regions).
2.Primary contact has First name,Last name .... and organization ID(FK from organizations)
3.Regions has Region ID,Name and Description,
4.Renewals has Renewal ID(PK)..... and Organization ID( FK from organizations).
 
and here I am passing this renewals data to VIEW here I wanto show,
 
Organization Name,Fist and Last name, and Region name in MVC view in table format.
 
to accomplish above functionality i have written below code.
  1. IEnumerable model = new List();      
  2. try      
  3. {      
  4. model = context.Set().ToList().Select(s => new AllRenewalsVM      
  5. {      
  6. OrganizationName = getOrgnizationName(s.OrgnizationId),      
  7. RenewalId = s.RenewalId,      
  8. ContactPersonName = getName(s.OrgnizationId),      
  9. RegionName = getRegion(s.OrgnizationId),       
  10. });      
  11.       
  12. return View("AllRenewals", model);     
  13. public string getOrgnizationName(Guid orgnizationId)    
  14. {    
  15. string orgName = "";    
  16. try    
  17. {    
  18. var orgnizationName = context.set.Where(s => s.OrgnizationId == orgnizationId).FirstOrDefault();    
  19. if (orgnizationName.IsDeleted == null)    
  20. {    
  21. orgName = orgnizationName.Name.ToString();    
  22. }    
  23. }    
  24. catch (Exception e)    
  25. {    
  26. }    
  27. return orgName;    
  28. }     
  29. public string getName(Guid orgnizationId)    
  30. {    
  31. string Name = "";    
  32. try    
  33. {    
  34. var Names = context.set().Where(s => s.OrgnizationId == orgnizationId).FirstOrDefault();    
  35. orgnizationId).FirstOrDefault();    
  36. if (Names.IsDeleted == null)    
  37. {    
  38. Name = Names.FirstName + " " + Names.LastName.ToString();    
  39. }    
  40. }    
  41. catch (Exception e)    
  42. {    
  43. }    
  44. return Name;    
  45. }    
  46.      
  47.      
  48. public string getRegion(Guid orgnizationId)    
  49. {    
  50. string Name = "";    
  51. try    
  52. {    
  53. var org = context.Set().Where(s => s.OrgnizationId == orgnizationId).FirstOrDefault();    
  54. var regionName = context.Set().Where(s => s.RegionId == org.RegionId).FirstOrDefault();    
  55. if (regionName != null)    
  56. {    
  57. Name = regionName.Name.ToString();    
  58. }    
  59. }    
  60. catch (Exception e)    
  61. {    
  62. }    
  63. return Name;    
  64. }   
@* Including Model to VIEW *@
  1. @model IEnumerable  
  2. @{  
  3. ViewData["Title"] = "Renewals";  
  4. Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
@* Binding data to HTML *@
  1. class="table table-striped dt-responsive display">  
  2. Name  
  3. Organization  
  4. Region  
  5. @foreach (var item in Model)  
  6. {  
  7. @Html.DisplayFor(modelItem => item.ContactPersonName)  
  8. @Html.DisplayFor(modelItem => item.OrganizationName)  
  9. @Html.DisplayFor(modelItem => item.RegionName)  
  10. }  
This is my code which i used to show data on page,
 
this code is working very fine but it couses skow performance issue.
 
is there are any better way to pass data from multiple dependent table?

Answers (1)