hohohotrucken23490

Untitled

Nov 4th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. /* a snippet of my controller class that displays the card views */
  2. [AllowAnonymous]
  3. public IActionResult JumpToDashboard()
  4. {
  5.  
  6. var listingUp = _context.ListingDBTable
  7. .Select(u => new ListingProjects()
  8. {
  9. ListingName = "Keong Saik Road 8",
  10. ImageUrl = "someurl",
  11. CategoryId = 1,
  12. LocationId = 1
  13. }).ToList();
  14. //JSON can refer this
  15. var listingProject = new ListingProjects
  16. {
  17. ListingName = "Keong Saik Road 8",
  18. ImageUrl = "someurl",
  19. CategoryId = 1,
  20. LocationId = 1
  21.  
  22. };
  23.  
  24. //Access category and location information
  25. var categoryName = new Category
  26. {
  27. propertyName = "Holiday Suites",
  28. propertyType = "hotel establishment"
  29. };
  30.  
  31. var city = new Location
  32. {
  33. City = "Venezia",
  34. State = "Venezia",
  35. PLZ = "2349890"
  36. };
  37. ViewData["PropertyListings"] = listingProject;
  38. ViewData["ListingCategories"] = categoryName;
  39. ViewData["Cities"] = city;
  40.  
  41. return View(listingUp);
  42. }
  43.  
  44. [HttpPost]
  45. public IActionResult Delete(int id)
  46. {
  47. try
  48. {
  49. var listingToBeDeleted = _context.ListingDBTable.Find(id);
  50. if (listingToBeDeleted != null)
  51. {
  52. _context.ListingDBTable.Remove(listingToBeDeleted);
  53. _context.SaveChanges();
  54. return Json("success"); // Or any success message
  55. }
  56. return Json("Not found"); // Or appropriate error message
  57. }
  58. catch (Exception ex)
  59. {
  60. return Json("An error occurred: " + ex.Message);
  61. }
  62. }
  63.  
  64.  
  65. /* view */
  66. @model WebApplication1.Models.ListingProjects
  67. @{
  68. ViewData["Title"] = "insert listings";
  69. }
  70.  
  71. <!DOCTYPE html>
  72.  
  73. <html>
  74. <head>
  75. <meta name="viewport" content="width=device-width" />
  76. <title>Index</title>
  77. </head>
  78. <body>
  79.  
  80. @Html.AntiForgeryToken()
  81.  
  82. <table cellpadding="0" cellspacing="0">
  83. <tr>
  84. <td>ID: </td>
  85. <td>@Model.Id</td> <!-- Ensure the ID is accessible here -->
  86. </tr>
  87. <tr>
  88. <td>Name: </td>
  89. <td>
  90. @Html.TextBoxFor(m => m.ListingName)
  91. </td>
  92. </tr>
  93. <tr>
  94. <td>Location: </td>
  95. <td>
  96. @Html.TextBoxFor(m => m.Location)
  97. </td>
  98. </tr>
  99. <tr>
  100. <td><input type="submit" name="submit" value="Save"/></td>
  101. <td><button class="delete">Delete</button></td>
  102. </tr>
  103. </table>
  104.  
  105. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
  106. @if (Model != null)
  107. {
  108. <script type="text/javascript">
  109.  
  110. $(document).ready(function () {
  111. $(".delete").click(function (e) {
  112. e.preventDefault(); // Prevent default form submission
  113.  
  114. var parent = $(this).parents("tr");
  115. var id = parent.find("td:eq(1)").text(); // Get ID from table
  116.  
  117. $.ajax({
  118. type: "POST",
  119. url: "@Url.Action("Delete", "Home")", // Corrected controller name
  120. data: {
  121. id: id,
  122. __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val() // Anti-forgery token
  123. },
  124. dataType: "json", // Expect JSON response (optional)
  125. success: function (data, status, xhr) {
  126. if (data === "success") {
  127. parent.hide("slow"); // Hide row on success
  128. } else {
  129. alert("Error: " + data); // Display error message
  130. }
  131. },
  132. error: function (xhr, status, error) {
  133. console.log("Error: " + status + " " + error + " " + xhr.status + " " + xhr.statusText);
  134. }
  135. });
  136. });
  137. });
  138. </script>
  139. }
  140.  
  141. </body>
  142. </html>
  143.  
  144. /*DB context class*/
  145. public class ApplicationDbContext : IdentityDbContext<PortalUsers>
  146. {
  147. public ApplicationDbContext(){}
  148. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
  149. : base(options)
  150. {
  151. }
  152.  
  153. public Microsoft.EntityFrameworkCore.DbSet<PortalUsers> UsersDBTable { get; set; }
  154. public Microsoft.EntityFrameworkCore.DbSet<Category> CategoriesDBTable { get; set; }
  155. public Microsoft.EntityFrameworkCore.DbSet<ListingProjects> ListingDBTable { get; set; }
  156. public Microsoft.EntityFrameworkCore.DbSet<Location> LocationDBTable { get; set; }
  157.  
  158.  
  159. }
  160.  
  161. /*Model class*/
  162. namespace WebApplication1.Models;
  163.  
  164. public class ListingProjects
  165. {
  166. //public string? inputsToDB { get; set; }
  167. public int? Id { get; set; }
  168. public string? ListingName { get; set; }
  169. public string? ImageUrl { get; set; }
  170.  
  171. //Navigation properties to child model classes
  172. public int? CategoryId { get; set; }
  173. public Category? Category { get; set; }
  174.  
  175. public int? LocationId { get; set; }
  176. public Location? Location { get; set; }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment