hohohotrucken23490

Untitled

Oct 13th, 2024
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. /*the view (htmlcs) in question that displays no listings available if it thinks my model is null*/
  2. @model IEnumerable<WebApplication1.Models.ListingProjects>
  3.  
  4. @{
  5. ViewData["Title"] = "Dashboard";
  6. var listingMd = Model?.First();
  7. }
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <meta name="viewport" content="width=device-width" />
  12. <title>TestDashboard1</title>
  13. <style>
  14. .listing { margin-bottom: 20px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; }
  15. .listing img { width: 100px; height: auto; }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="listings-container"></div>
  20. @if (listingMd != null)
  21. {
  22. <p>listing is not null</p>
  23. <h2>Test index: @(listingMd.ListingName ?? "null null")</h2> <!-- Use null-coalescing operator for ListingName -->
  24. <p>Age: @(listingMd.Id != null ? listingMd.Id.ToString() : "Not Provided")</p>
  25. <p>Location: @(listingMd.CategoryId != null ? listingMd.CategoryId.ToString() : "Not Provided")</p>
  26. }
  27. else
  28. {
  29. <p>No listings available.</p> <!-- Fallback content if Model is empty -->
  30. }
  31.  
  32. /*the model*/
  33. namespace WebApplication1.Models;
  34.  
  35. public class ListingProjects
  36. {
  37. public int? Id { get; set; }
  38. public string? ListingName { get; set; }
  39. public string? ImageUrl { get; set; }
  40.  
  41. //Navigation properties to child model classes
  42. public int? CategoryId { get; set; }
  43. public Category? Category { get; set; }
  44.  
  45. public int? LocationId { get; set; }
  46. public Location? Location { get; set; }
  47. }
  48.  
  49. /* controller */
  50. private readonly HttpClient _httpClient;
  51. private readonly ApplicationDbContext _context;
  52.  
  53. public HomeController(HttpClient httpClient, ApplicationDbContext context)
  54. {
  55. _httpClient = httpClient;
  56. _context = context;
  57. }
  58.  
  59. [AllowAnonymous]
  60. public IActionResult JumpToDashboard()
  61. {
  62. var listingUp = _context.ListingDBTable
  63. .Select(u => new WebApplication1.Models.ListingProjects()
  64. {
  65. ListingName = "Keong Saik Road 8",
  66. ImageUrl = "",
  67. CategoryId = 1,
  68. LocationId = 1
  69. }).ToList();
  70. //JSON can refer this
  71. var listingProject = new ListingProjects
  72. {
  73. ListingName = "Keong Saik Road 8",
  74. ImageUrl = "",
  75. CategoryId = 1,
  76. LocationId = 1
  77.  
  78. };
  79.  
  80. //Access category and location information
  81. var categoryName = new Category
  82. {
  83. propertyName = "Holiday Suites",
  84. propertyType = "hotel establishment"
  85. };
  86.  
  87. var city = new Location
  88. {
  89. City = "Venezia",
  90. State = "Venezia",
  91. PLZ = "2349890"
  92. };
  93. ViewData["PropertyListings"] = listingProject;
  94. ViewData["ListingCategories"] = categoryName;
  95. ViewData["Cities"] = city;
  96.  
  97. //add try catch
  98. return View(listingUp);
  99. }
  100.  
  101. /*ApplicationDbContext class */
  102. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  103. using Microsoft.EntityFrameworkCore;
  104. using WebApplication1.Models;
  105.  
  106. namespace WebApplication1.Data;
  107.  
  108. public class ApplicationDbContext : IdentityDbContext<PortalUsers>
  109. {
  110. public ApplicationDbContext(){}
  111. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
  112. : base(options)
  113. {
  114. }
  115.  
  116. public Microsoft.EntityFrameworkCore.DbSet<PortalUsers> UsersDBTable { get; set; }
  117. public Microsoft.EntityFrameworkCore.DbSet<Category> CategoriesDBTable { get; set; }
  118. public Microsoft.EntityFrameworkCore.DbSet<ListingProjects> ListingDBTable { get; set; }
  119. public Microsoft.EntityFrameworkCore.DbSet<Location> LocationDBTable { get; set; }
  120.  
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment