Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*the view (htmlcs) in question that displays no listings available if it thinks my model is null*/
- @model IEnumerable<WebApplication1.Models.ListingProjects>
- @{
- ViewData["Title"] = "Dashboard";
- var listingMd = Model?.First();
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>TestDashboard1</title>
- <style>
- .listing { margin-bottom: 20px; padding: 10px; border: 1px solid #ccc; border-radius: 5px; }
- .listing img { width: 100px; height: auto; }
- </style>
- </head>
- <body>
- <div id="listings-container"></div>
- @if (listingMd != null)
- {
- <p>listing is not null</p>
- <h2>Test index: @(listingMd.ListingName ?? "null null")</h2> <!-- Use null-coalescing operator for ListingName -->
- <p>Age: @(listingMd.Id != null ? listingMd.Id.ToString() : "Not Provided")</p>
- <p>Location: @(listingMd.CategoryId != null ? listingMd.CategoryId.ToString() : "Not Provided")</p>
- }
- else
- {
- <p>No listings available.</p> <!-- Fallback content if Model is empty -->
- }
- /*the model*/
- namespace WebApplication1.Models;
- public class ListingProjects
- {
- public int? Id { get; set; }
- public string? ListingName { get; set; }
- public string? ImageUrl { get; set; }
- //Navigation properties to child model classes
- public int? CategoryId { get; set; }
- public Category? Category { get; set; }
- public int? LocationId { get; set; }
- public Location? Location { get; set; }
- }
- /* controller */
- private readonly HttpClient _httpClient;
- private readonly ApplicationDbContext _context;
- public HomeController(HttpClient httpClient, ApplicationDbContext context)
- {
- _httpClient = httpClient;
- _context = context;
- }
- [AllowAnonymous]
- public IActionResult JumpToDashboard()
- {
- var listingUp = _context.ListingDBTable
- .Select(u => new WebApplication1.Models.ListingProjects()
- {
- ListingName = "Keong Saik Road 8",
- ImageUrl = "",
- CategoryId = 1,
- LocationId = 1
- }).ToList();
- //JSON can refer this
- var listingProject = new ListingProjects
- {
- ListingName = "Keong Saik Road 8",
- ImageUrl = "",
- CategoryId = 1,
- LocationId = 1
- };
- //Access category and location information
- var categoryName = new Category
- {
- propertyName = "Holiday Suites",
- propertyType = "hotel establishment"
- };
- var city = new Location
- {
- City = "Venezia",
- State = "Venezia",
- PLZ = "2349890"
- };
- ViewData["PropertyListings"] = listingProject;
- ViewData["ListingCategories"] = categoryName;
- ViewData["Cities"] = city;
- //add try catch
- return View(listingUp);
- }
- /*ApplicationDbContext class */
- using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
- using Microsoft.EntityFrameworkCore;
- using WebApplication1.Models;
- namespace WebApplication1.Data;
- public class ApplicationDbContext : IdentityDbContext<PortalUsers>
- {
- public ApplicationDbContext(){}
- public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
- : base(options)
- {
- }
- public Microsoft.EntityFrameworkCore.DbSet<PortalUsers> UsersDBTable { get; set; }
- public Microsoft.EntityFrameworkCore.DbSet<Category> CategoriesDBTable { get; set; }
- public Microsoft.EntityFrameworkCore.DbSet<ListingProjects> ListingDBTable { get; set; }
- public Microsoft.EntityFrameworkCore.DbSet<Location> LocationDBTable { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment