Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* a snippet of my controller class that displays the card views */
- [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;
- return View(listingUp);
- }
- [HttpPost]
- public IActionResult Delete(int id)
- {
- try
- {
- var listingToBeDeleted = _context.ListingDBTable.Find(id);
- if (listingToBeDeleted != null)
- {
- _context.ListingDBTable.Remove(listingToBeDeleted);
- _context.SaveChanges();
- return Json("success"); // Or any success message
- }
- return Json("Not found"); // Or appropriate error message
- }
- catch (Exception ex)
- {
- return Json("An error occurred: " + ex.Message);
- }
- }
- /* view */
- @model WebApplication1.Models.ListingProjects
- @{
- ViewData["Title"] = "insert listings";
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- @Html.AntiForgeryToken()
- <table cellpadding="0" cellspacing="0">
- <tr>
- <td>ID: </td>
- <td>@Model.Id</td> <!-- Ensure the ID is accessible here -->
- </tr>
- <tr>
- <td>Name: </td>
- <td>
- @Html.TextBoxFor(m => m.ListingName)
- </td>
- </tr>
- <tr>
- <td>Location: </td>
- <td>
- @Html.TextBoxFor(m => m.Location)
- </td>
- </tr>
- <tr>
- <td><input type="submit" name="submit" value="Save"/></td>
- <td><button class="delete">Delete</button></td>
- </tr>
- </table>
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
- @if (Model != null)
- {
- <script type="text/javascript">
- $(document).ready(function () {
- $(".delete").click(function (e) {
- e.preventDefault(); // Prevent default form submission
- var parent = $(this).parents("tr");
- var id = parent.find("td:eq(1)").text(); // Get ID from table
- $.ajax({
- type: "POST",
- url: "@Url.Action("Delete", "Home")", // Corrected controller name
- data: {
- id: id,
- __RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val() // Anti-forgery token
- },
- dataType: "json", // Expect JSON response (optional)
- success: function (data, status, xhr) {
- if (data === "success") {
- parent.hide("slow"); // Hide row on success
- } else {
- alert("Error: " + data); // Display error message
- }
- },
- error: function (xhr, status, error) {
- console.log("Error: " + status + " " + error + " " + xhr.status + " " + xhr.statusText);
- }
- });
- });
- });
- </script>
- }
- </body>
- </html>
- /*DB context class*/
- 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; }
- }
- /*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; }
- }
Advertisement
Add Comment
Please, Sign In to add comment