hohohotrucken23490

Untitled

Nov 3rd, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. /*Controller Class*/
  2. using System.Diagnostics;
  3. using Microsoft.AspNetCore.Mvc;
  4. using WebApplication1.Models;
  5. using Microsoft.AspNetCore.Authorization;
  6. using WebApplication1.Data;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.EntityFrameworkCore;
  9.  
  10. namespace WebApplication1.Controllers;
  11.  
  12.  
  13. public class HomeController : Controller
  14. {
  15. private readonly HttpClient _httpClient;
  16. private readonly ApplicationDbContext _context;
  17.  
  18. public HomeController(HttpClient httpClient, ApplicationDbContext context)
  19. {
  20. _httpClient = httpClient;
  21. _context = context;
  22. }
  23.  
  24. [Authorize]
  25. public IActionResult Index()
  26. {
  27. var portalUserModelRef = _context.UsersDBTable
  28. .Select(u => new WebApplication1.Models.PortalUsers
  29. {
  30. Name = u.Name,
  31. Age = u.Age,
  32. Location = u.Location
  33. }).ToList();
  34.  
  35. return View(portalUserModelRef);
  36. }
  37.  
  38. [AllowAnonymous]
  39. public IActionResult InsertListings(String inputsToDB, int IntegerInputsToDB)
  40. {
  41.  
  42.  
  43.  
  44. // Assuming you want to return the newly created ListingProject
  45. var newListing = new ListingProjects
  46. {
  47. ListingName = inputsToDB,
  48. //CategoryId = IntegerInputsToDB,
  49. //LocationId = IntegerInputsToDB
  50. };
  51.  
  52.  
  53.  
  54. _context.ListingDBTable.Add(newListing);
  55. _context.SaveChanges();
  56.  
  57. return View(newListing);
  58.  
  59. }
  60.  
  61. [HttpPost]
  62. [ValidateAntiForgeryToken]
  63. public ActionResult SaveNotes(ListingProjects listingProject) // Change to ListingProjects model
  64. {
  65. if (ModelState.IsValid)
  66. {
  67. _context.ListingDBTable.Add(listingProject);
  68. _context.SaveChanges();
  69. return RedirectToAction("TestDashboard1");
  70. }
  71.  
  72. // Model is invalid, handle it (e.g., show validation errors)
  73. return View(listingProject);
  74. }
  75.  
  76. public IActionResult Privacy()
  77. {
  78. return View();
  79. }
  80.  
  81. [AllowAnonymous]
  82. public IActionResult JumpToDashboard()
  83. {
  84.  
  85. var listingUp = _context.ListingDBTable
  86. .Select(u => new ListingProjects()
  87. {
  88. ListingName = "Keong Saik Road 8",
  89. ImageUrl = "someurl",
  90. CategoryId = 1,
  91. LocationId = 1
  92. }).ToList();
  93. //JSON can refer this
  94. var listingProject = new ListingProjects
  95. {
  96. ListingName = "Keong Saik Road 8",
  97. ImageUrl = "someurl",
  98. CategoryId = 1,
  99. LocationId = 1
  100.  
  101. };
  102.  
  103. //Access category and location information
  104. var categoryName = new Category
  105. {
  106. propertyName = "Holiday Suites",
  107. propertyType = "hotel establishment"
  108. };
  109.  
  110. var city = new Location
  111. {
  112. City = "Venezia",
  113. State = "Venezia",
  114. PLZ = "2349890"
  115. };
  116. ViewData["PropertyListings"] = listingProject;
  117. ViewData["ListingCategories"] = categoryName;
  118. ViewData["Cities"] = city;
  119.  
  120. foreach (var listing in listingUp)
  121. {
  122. listing.ListingName = Url.Action("DeleteListing", new { id = listing.ListingName });
  123. listing.ImageUrl = Url.Action("DeleteListing", new { id = listing.ImageUrl });
  124.  
  125. listing.CategoryId = Url.Action("DeleteListing", new { id = listing.CategoryId });
  126. listing.LocationId = Url.Action("DeleteListing", new { id = listing.LocationId });// Assuming Id is the property to identify the listing
  127. }
  128.  
  129.  
  130. //add try catch
  131. return View(listingUp);
  132. }
  133.  
  134.  
  135.  
  136. [AllowAnonymous]
  137. public async Task<IActionResult> TestDashboard1()
  138. {
  139. var datasource= _context.ListingDBTable.AsQueryable();
  140. var query = datasource.Include(x => x.Location);
  141. var listings= await query.ToListAsync();
  142.  
  143. return View(listings);
  144. }
  145.  
  146. [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  147. public IActionResult Error()
  148. {
  149. return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  150. }
  151. }
  152.  
  153. /*Model class*/
  154. namespace WebApplication1.Models;
  155.  
  156. public class ListingProjects
  157. {
  158. //public string? inputsToDB { get; set; }
  159. public int? Id { get; set; }
  160. public string? ListingName { get; set; }
  161. public string? ImageUrl { get; set; }
  162.  
  163. //Navigation properties to child model classes
  164. public int? CategoryId { get; set; }
  165. public Category? Category { get; set; }
  166.  
  167. public int? LocationId { get; set; }
  168. public Location? Location { get; set; }
  169. }
  170.  
  171. /*the view where I wanted to add the delete btn*/
  172.  
  173. @{
  174. ViewData["Title"] = "Dashboard";
  175. //var listingMd = Model?.FirstOrDefault();
  176. }
  177. <!DOCTYPE html>
  178. <html>
  179. <head>
  180. <meta name="viewport" content="width=device-width" />
  181. <title>TestDashboard1</title>
  182. <style>
  183. .listing {
  184. margin: 10px;
  185. padding: 10px;
  186. width: 45%; /* Make each listing take up roughly half the container */
  187. border: 1.5px outset darkgray;
  188. box-shadow: 0 0 2px #aca9a9;
  189. border-radius: 6px;
  190. }
  191. .wrapper {
  192. display: flex;
  193. flex-wrap: wrap; /* Allows listings to wrap to the next line if necessary */
  194. gap: 10px; /* Adds spacing between listing items */
  195. }
  196. .listing img {
  197. width: 100%;
  198. height: auto;
  199. }
  200. .roundPlaceholder {
  201. width: 60px;
  202. height: 20px;
  203. background: #a84909;
  204. border-radius: 10px;
  205. }
  206. .roundPlaceholderTxt {
  207. font-size: 10px;
  208. font-family: Arial;
  209. text-align: center;
  210. color: aliceblue;
  211. }
  212. .listingPhoto {
  213. text-align: center;
  214. width: 100%;
  215. }
  216. </style>
  217. </head>
  218. <body>
  219. <div class="wrapper">
  220. @foreach (var listingMd in Model)
  221. {
  222. @if (listingMd != null)
  223. {
  224. <div class="listing">
  225. <p>Listing is not null</p>
  226. <div><img class="listingPhoto" src="~/imgSearchHome/IMG_20220102_161928.jpg" /></div>
  227. <div>
  228. <p>Test index: @(listingMd.ListingName ?? "null null")</p>
  229. <p>Age: @(listingMd.Id != null ? listingMd.Id.ToString() : "Not Provided")</p>
  230. <div class="roundPlaceholder">
  231. <p class="roundPlaceholderTxt">Book me</p>
  232. </div>
  233. <div class="deleteListingBtn">
  234. <a href="@listingMd.ListingName, @listingMd.CategoryId" class="btn btn-danger">Delete</a>
  235. </div>
  236.  
  237. <p>Location: @(listingMd.CategoryId != null ? listingMd.CategoryId.ToString() : "Not Provided")</p>
  238. </div>
  239. </div>
  240. }
  241. else
  242. {
  243. <p>No listings available.</p>
  244. }
  245. }
  246. </div>
  247. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
  248. }
  249. </body>
  250. </html>
  251.  
  252. /*DB class*/
  253. using System.Data.Entity;
  254. using Microsoft.AspNetCore.Identity;
  255. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  256. using Microsoft.EntityFrameworkCore;
  257. using WebApplication1.Models;
  258.  
  259. namespace WebApplication1.Data;
  260.  
  261. public class ApplicationDbContext : IdentityDbContext<PortalUsers>
  262. {
  263. public ApplicationDbContext(){}
  264. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
  265. : base(options)
  266. {
  267. }
  268.  
  269. public Microsoft.EntityFrameworkCore.DbSet<PortalUsers> UsersDBTable { get; set; }
  270. public Microsoft.EntityFrameworkCore.DbSet<Category> CategoriesDBTable { get; set; }
  271. public Microsoft.EntityFrameworkCore.DbSet<ListingProjects> ListingDBTable { get; set; }
  272. public Microsoft.EntityFrameworkCore.DbSet<Location> LocationDBTable { get; set; }
  273.  
  274.  
  275. }
Advertisement
Add Comment
Please, Sign In to add comment