Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Controller*/
- [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();
- return View(newListing);
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult SaveListings(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);
- }
- /* View */
- @model ListingProjects
- @{
- ViewData["Title"] = "insert listings";
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- @Html.AntiForgeryToken()
- <table id="tblCust" class="table table-bordered">
- <thead>
- <tr>
- <td>@Html.LabelFor(model => model.ListingName, new { @class = "control-label" })</td>
- </tr>
- <tr>
- <td>@Html.TextBoxFor(model => model.ListingName, new { @class = "form-control" })</td>
- </tr>
- <tr>
- <td><button style="float:right;" type="submit" data-entity-id="@Model.Id" class="btn btn-info">Save</button></td>
- </tr>
- </thead>
- <tbody></tbody>
- </table>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script>
- $(document).ready(function() {
- $("@Model.Id").click(function(){
- var val = $(this).val();
- $.ajax({
- type:"POST",
- url: '@Url.Action("SaveListings", "Home")?IntegrationPoint=' + val,
- dataType: 'json',
- data:{String:val}
- })
- .fail(function(){alert("fail")})
- .done(function(){alert("success")})
- });
- });
- </script>
- </body>
- </html>
- /*Model*/
- using System.Collections;
- namespace WebApplication1.Models;
- public class ListingProjects : IEnumerable
- {
- //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; }
- // Implement IEnumerable correctly
- public IEnumerator<ListingProjects> GetEnumerator()
- {
- yield return this;
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement