Guest User

Untitled

a guest
Feb 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. public class CompanyAdmin
  2. {
  3. public string Id { get; set; }
  4. [Required(AllowEmptyStrings = false)]
  5. [Display(Name = "Company Name")]
  6. public string CompanyName { get; set; }
  7. public virtual List<Certificate> Certificates { get; set; }
  8.  
  9.  
  10. public class Certificate
  11. {
  12. [Key]
  13. [Display(Name = "Certificate No.")]
  14. public string CertNo { get; set; }
  15. [Display(Name = "Company")]
  16. public IEnumerable<SelectListItem> Companies { get; set; }
  17. [Required]
  18. public string Company { get; set; }
  19. [Display(Name = "User")]
  20. public IEnumerable<SelectListItem> UserList { get; set; }
  21. [Required]
  22. public string User { get; set; }
  23. public string UploadedBy { get; set; }
  24. [Display(Name = "Description")]
  25. public string CertDescription { get; set; }
  26. [Display(Name = "File Location")]
  27. public string CertFileLocation { get; set; }
  28. [Display(Name = "Last Modified")]
  29. public DateTime LastModifiedDate { get; set; }
  30.  
  31. }
  32.  
  33. public class CertificateViewModel
  34. {
  35. [Display(Name = "Certificate No.")]
  36. public string CertNo { get; set; }
  37. public string User { get; set; }
  38. [Display(Name = "User")]
  39. public string UserName { get; set; }
  40. public string Company { get; set; }
  41. [Display(Name = "Company")]
  42. public string CompanyName { get; set; }
  43. [Display(Name = "Description")]
  44. public string CertDescription { get; set; }
  45. [Display(Name = "Last Modified")]
  46. public DateTime LastModifiedDate { get; set; }
  47. }
  48.  
  49. public ActionResult Index()
  50. {
  51. if (User.IsInRole("Admin"))
  52. {
  53. IEnumerable<CertificateViewModel> model = null;
  54.  
  55. model = (from c in db.CertificateDB
  56. join u in db.Users on c.User equals u.Id
  57. join d in db.CompanyDB on c.Company equals d.Id
  58. select new CertificateViewModel
  59. {
  60. CertNo = c.CertNo,
  61. UserName = u.UserName,
  62. CompanyName = d.CompanyName,
  63. CertDescription = c.CertDescription,
  64. LastModifiedDate = c.LastModifiedDate
  65. }
  66. );
  67. return View(model);
  68. }
  69. else
  70. {
  71. return RedirectToAction("Index", "Home");
  72. }
  73.  
  74. }
  75.  
  76. @model IEnumerable<IdentitySample.Models.CertificateViewModel>
  77. <table class="table">
  78. <tr>
  79. <th>
  80. @Html.DisplayNameFor(model => model.CertNo)
  81. </th>
  82. <th>
  83. @Html.DisplayNameFor(model => model.Company)
  84. </th>
  85. <th>
  86. @Html.DisplayNameFor(model => model.User)
  87. </th>
  88. <th>
  89. @Html.DisplayNameFor(model => model.CertDescription)
  90. </th>
  91. <th>
  92. @Html.DisplayNameFor(model => model.LastModifiedDate)
  93. </th>
  94. <th></th>
  95. </tr>
  96.  
  97. @foreach (var item in Model)
  98. {
  99. <tr>
  100. <td>
  101. @Html.DisplayFor(modelItem => item.CertNo)
  102. </td>
  103. <td>
  104. @Html.DisplayFor(modelItem => item.Company)
  105. </td>
  106. <td>
  107. @Html.DisplayFor(modelItem => item.User)
  108. </td>
  109. <td>
  110. @Html.DisplayFor(modelItem => item.CertDescription)
  111. </td>
  112. <td>
  113. @Html.DisplayFor(modelItem => item.LastModifiedDate)
  114. </td>
  115. <td>
  116. @Html.ActionLink("Edit", "Edit", new { id = item.CertNo }) |
  117. @Html.ActionLink("Details", "Details", new { id = item.CertNo }) |
  118. @Html.ActionLink("Delete", "Delete", new { id = item.CertNo })
  119. </td>
  120. </tr>
  121. }
  122.  
  123. </table>
  124.  
  125. public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
  126. {
  127. public ApplicationDbContext()
  128. : base("DefaultConnection", throwIfV1Schema: false)
  129. {
  130. }
  131.  
  132. static ApplicationDbContext()
  133. {
  134. // Set the database intializer which is run once during application start
  135. // This seeds the database with admin user credentials and admin role
  136.  
  137. Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
  138. }
  139.  
  140. public override int SaveChanges()
  141. {
  142. try
  143. {
  144. return base.SaveChanges();
  145. }
  146. catch (DbEntityValidationException ex)
  147. {
  148. string errorMessages = string.Join("; ", ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.PropertyName + ": " + x.ErrorMessage));
  149. throw new DbEntityValidationException(errorMessages);
  150. }
  151. }
  152.  
  153. public DbSet<CompanyAdmin> CompanyDB { get; set; }
  154. public DbSet<Certificate> CertificateDB { get; set; }
  155.  
  156. public static ApplicationDbContext Create()
  157. {
  158. return new ApplicationDbContext();
  159. }
  160. }
Add Comment
Please, Sign In to add comment