Advertisement
hohohotrucken23490

Untitled

Nov 8th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. /*Controller*/
  2. [AllowAnonymous]
  3. public IActionResult InsertListings(String inputsToDB, int IntegerInputsToDB)
  4. {
  5.  
  6.  
  7.  
  8. // Assuming you want to return the newly created ListingProject
  9. var newListing = new ListingProjects
  10. {
  11. ListingName = inputsToDB,
  12. //CategoryId = IntegerInputsToDB,
  13. //LocationId = IntegerInputsToDB
  14. };
  15.  
  16.  
  17.  
  18. _context.ListingDBTable.Add(newListing);
  19. _context.SaveChanges();
  20.  
  21. //return View();
  22.  
  23. return View(newListing);
  24.  
  25. }
  26.  
  27. [HttpPost]
  28. [ValidateAntiForgeryToken]
  29. public ActionResult SaveListings(ListingProjects listingProject) // Change to ListingProjects model
  30. {
  31. if (ModelState.IsValid)
  32. {
  33. _context.ListingDBTable.Add(listingProject);
  34. _context.SaveChanges();
  35. return RedirectToAction("TestDashboard1");
  36. }
  37.  
  38. // Model is invalid, handle it (e.g., show validation errors)
  39. return View(listingProject);
  40. }
  41.  
  42. /* View */
  43. @model ListingProjects
  44.  
  45. @{
  46. ViewData["Title"] = "insert listings";
  47. }
  48.  
  49. <!DOCTYPE html>
  50. <html>
  51. <head>
  52. <meta name="viewport" content="width=device-width" />
  53. <title>Index</title>
  54. </head>
  55. <body>
  56.  
  57. @Html.AntiForgeryToken()
  58.  
  59. <table id="tblCust" class="table table-bordered">
  60. <thead>
  61. <tr>
  62. <td>@Html.LabelFor(model => model.ListingName, new { @class = "control-label" })</td>
  63. </tr>
  64. <tr>
  65. <td>@Html.TextBoxFor(model => model.ListingName, new { @class = "form-control" })</td>
  66. </tr>
  67. <tr>
  68. <td><button style="float:right;" type="submit" data-entity-id="@Model.Id" class="btn btn-info">Save</button></td>
  69. </tr>
  70. </thead>
  71. <tbody></tbody>
  72. </table>
  73.  
  74. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  75. <script>
  76. $(document).ready(function() {
  77. $("@Model.Id").click(function(){
  78. var val = $(this).val();
  79.  
  80. $.ajax({
  81. type:"POST",
  82. url: '@Url.Action("SaveListings", "Home")?IntegrationPoint=' + val,
  83. dataType: 'json',
  84. data:{String:val}
  85. })
  86. .fail(function(){alert("fail")})
  87. .done(function(){alert("success")})
  88.  
  89. });
  90. });
  91. </script>
  92. </body>
  93. </html>
  94.  
  95. /*Model*/
  96. using System.Collections;
  97.  
  98. namespace WebApplication1.Models;
  99.  
  100. public class ListingProjects : IEnumerable
  101. {
  102. //public string? inputsToDB { get; set; }
  103. public int? Id { get; set; }
  104. public string? ListingName { get; set; }
  105. public string? ImageUrl { get; set; }
  106.  
  107. //Navigation properties to child model classes
  108. public int? CategoryId { get; set; }
  109. public Category? Category { get; set; }
  110.  
  111. public int? LocationId { get; set; }
  112. public Location? Location { get; set; }
  113. // Implement IEnumerable correctly
  114. public IEnumerator<ListingProjects> GetEnumerator()
  115. {
  116. yield return this;
  117. }
  118.  
  119. IEnumerator IEnumerable.GetEnumerator()
  120. {
  121. return GetEnumerator();
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement