Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Controller Class*/
- using System.Diagnostics;
- using Microsoft.AspNetCore.Mvc;
- using WebApplication1.Models;
- using Microsoft.AspNetCore.Authorization;
- using WebApplication1.Data;
- using Microsoft.AspNetCore.Http;
- using Microsoft.EntityFrameworkCore;
- namespace WebApplication1.Controllers;
- public class HomeController : Controller
- {
- private readonly HttpClient _httpClient;
- private readonly ApplicationDbContext _context;
- public HomeController(HttpClient httpClient, ApplicationDbContext context)
- {
- _httpClient = httpClient;
- _context = context;
- }
- [Authorize]
- public IActionResult Index()
- {
- var portalUserModelRef = _context.UsersDBTable
- .Select(u => new WebApplication1.Models.PortalUsers
- {
- Name = u.Name,
- Age = u.Age,
- Location = u.Location
- }).ToList();
- return View(portalUserModelRef);
- }
- [AllowAnonymous]
- public IActionResult InsertListings(String inputsToDB, int IntegerInputsToDB)
- {
- // Assuming you want to return the newly created ListingProject
- var newListing = new ListingProjects
- {
- ListingName = inputsToDB,
- //CategoryId = IntegerInputsToDB,
- //LocationId = IntegerInputsToDB
- };
- _context.ListingDBTable.Add(newListing);
- _context.SaveChanges();
- return View(newListing);
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult SaveNotes(ListingProjects listingProject) // Change to ListingProjects model
- {
- if (ModelState.IsValid)
- {
- _context.ListingDBTable.Add(listingProject);
- _context.SaveChanges();
- return RedirectToAction("TestDashboard1");
- }
- // Model is invalid, handle it (e.g., show validation errors)
- return View(listingProject);
- }
- public IActionResult Privacy()
- {
- return View();
- }
- [AllowAnonymous]
- public IActionResult JumpToDashboard()
- {
- var listingUp = _context.ListingDBTable
- .Select(u => new ListingProjects()
- {
- ListingName = "Keong Saik Road 8",
- ImageUrl = "someurl",
- CategoryId = 1,
- LocationId = 1
- }).ToList();
- //JSON can refer this
- var listingProject = new ListingProjects
- {
- ListingName = "Keong Saik Road 8",
- ImageUrl = "someurl",
- 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;
- foreach (var listing in listingUp)
- {
- listing.ListingName = Url.Action("DeleteListing", new { id = listing.ListingName });
- listing.ImageUrl = Url.Action("DeleteListing", new { id = listing.ImageUrl });
- listing.CategoryId = Url.Action("DeleteListing", new { id = listing.CategoryId });
- listing.LocationId = Url.Action("DeleteListing", new { id = listing.LocationId });// Assuming Id is the property to identify the listing
- }
- //add try catch
- return View(listingUp);
- }
- [AllowAnonymous]
- public async Task<IActionResult> TestDashboard1()
- {
- var datasource= _context.ListingDBTable.AsQueryable();
- var query = datasource.Include(x => x.Location);
- var listings= await query.ToListAsync();
- return View(listings);
- }
- [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
- public IActionResult Error()
- {
- return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
- }
- }
- /*Model class*/
- namespace WebApplication1.Models;
- public class ListingProjects
- {
- //public string? inputsToDB { get; set; }
- 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; }
- }
- /*the view where I wanted to add the delete btn*/
- @{
- ViewData["Title"] = "Dashboard";
- //var listingMd = Model?.FirstOrDefault();
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>TestDashboard1</title>
- <style>
- .listing {
- margin: 10px;
- padding: 10px;
- width: 45%; /* Make each listing take up roughly half the container */
- border: 1.5px outset darkgray;
- box-shadow: 0 0 2px #aca9a9;
- border-radius: 6px;
- }
- .wrapper {
- display: flex;
- flex-wrap: wrap; /* Allows listings to wrap to the next line if necessary */
- gap: 10px; /* Adds spacing between listing items */
- }
- .listing img {
- width: 100%;
- height: auto;
- }
- .roundPlaceholder {
- width: 60px;
- height: 20px;
- background: #a84909;
- border-radius: 10px;
- }
- .roundPlaceholderTxt {
- font-size: 10px;
- font-family: Arial;
- text-align: center;
- color: aliceblue;
- }
- .listingPhoto {
- text-align: center;
- width: 100%;
- }
- </style>
- </head>
- <body>
- <div class="wrapper">
- @foreach (var listingMd in Model)
- {
- @if (listingMd != null)
- {
- <div class="listing">
- <p>Listing is not null</p>
- <div><img class="listingPhoto" src="~/imgSearchHome/IMG_20220102_161928.jpg" /></div>
- <div>
- <p>Test index: @(listingMd.ListingName ?? "null null")</p>
- <p>Age: @(listingMd.Id != null ? listingMd.Id.ToString() : "Not Provided")</p>
- <div class="roundPlaceholder">
- <p class="roundPlaceholderTxt">Book me</p>
- </div>
- <div class="deleteListingBtn">
- <a href="@listingMd.ListingName, @listingMd.CategoryId" class="btn btn-danger">Delete</a>
- </div>
- <p>Location: @(listingMd.CategoryId != null ? listingMd.CategoryId.ToString() : "Not Provided")</p>
- </div>
- </div>
- }
- else
- {
- <p>No listings available.</p>
- }
- }
- </div>
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
- }
- </body>
- </html>
- /*DB class*/
- using System.Data.Entity;
- using Microsoft.AspNetCore.Identity;
- 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