Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Model*/
- using System.Collections;
- using Microsoft.Build.Framework;
- namespace WebApplication1.Models;
- public class ListingProjects : IEnumerable
- {
- //public string? inputsToDB { get; set; }
- public int Id { get; set; }
- [Required]
- 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();
- }
- }
- /*DTO*/
- public class ListingProjectsDTO
- {
- [System.ComponentModel.DataAnnotations.Key]
- public int? ListingId { get; set; }
- public string? ListingName_DTO { get; set; }
- }
- /*Insert 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()
- @using (Html.BeginForm("InsertListings", "Home", FormMethod.Post))
- {
- <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><input type="submit" style="float:right;" data-entity-id="@Model?.Id" id="saveBtn" class="btn btn-info" value="Save"/></td>
- </tr>
- </thead>
- <tbody></tbody>
- </table>
- }
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script>
- var listingId = "@Model?.Id";
- </script>
- <script src="~/js/site.js"></script>
- </body>
- </html>
- /* API Controller*/
- [Route("api/[controller]")]
- [ApiController]
- public class APIController : Controller
- {
- private readonly ApplicationDbContext _context;
- public APIController(ApplicationDbContext context)
- {
- _context = context;
- }
- [HttpGet("availableListings")]
- public async Task<List<ListingProjectsDTO>> GetListings()
- {
- var listings = from b in _context.ListingDTO_DBTable
- select new ListingProjectsDTO()
- {
- ListingId = b.ListingId,
- ListingName_DTO = b.ListingName_DTO
- };
- return listings.ToList();
- }
- [ProducesResponseType(typeof(List<ListingProjectsDTO>), 200)]
- [HttpGet("{id}")]
- public async Task<IActionResult> Get(int id)
- {
- if (id < 1)
- return BadRequest();
- var product = await _context.ListingDBTable.FirstOrDefaultAsync(m => m.Id == id);
- if (product == null)
- return NotFound();
- return Ok(product);
- }
- [ProducesResponseType(typeof(List<ListingProjectsDTO>), 200)]
- [HttpPost]
- public async Task<IActionResult> Post(ListingProjects listing)
- {
- if (!ModelState.IsValid)
- {
- return BadRequest(ModelState);
- }
- _context.Add(listing);
- await _context.SaveChangesAsync();
- _context.Entry(listing).Reference(x => x.ListingName).Load();
- var dto = new ListingProjectsDTO()
- {
- ListingId = listing.Id,
- ListingName_DTO = listing.ListingName
- };
- return CreatedAtRoute("DefaultApi", new { id = listing.Id }, dto);
- }
- [HttpPut]
- public async Task<IActionResult> Put(ListingProjects listingData)
- {
- if (listingData == null || listingData.Id == 0)
- return BadRequest();
- var listingTask = await _context.ListingDBTable.FindAsync(listingData.Id);
- if (listingTask == null)
- return NotFound();
- listingTask.ListingName = listingData.ListingName;
- listingTask.ImageUrl = listingData.ImageUrl;
- listingTask.Category = listingData.Category;
- listingTask.Location = listingData.Location;
- await _context.SaveChangesAsync();
- return Ok();
- }
- [HttpDelete("{id}")]
- public async Task<IActionResult> Delete(int id)
- {
- if (id < 1)
- return BadRequest();
- var listingDel = await _context.ListingDBTable.FindAsync(id);
- if (listingDel == null)
- return NotFound();
- _context.ListingDBTable.Remove(listingDel);
- await _context.SaveChangesAsync();
- return Ok();
- }
- }
- /*HomeController, only insert functions listed*/
- [HttpGet]
- public IActionResult InsertListings()
- {
- return View();
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult InsertListings([Bind("ListingName")] ListingProjects listingProject) // Change to ListingProjects model
- {
- try
- {
- if (ModelState.IsValid)
- {
- _context.ListingDBTable.Add(listingProject);
- _context.SaveChanges();
- return RedirectToAction("TestDashboard1"); // Redirect to dashboard
- }
- else
- {
- // Model is invalid, handle it (e.g., show validation errors)
- return View(listingProject); // Return the view with error messages
- }
- }
- catch (DbUpdateException dbUpdateException)
- {
- ModelState.AddModelError("", "Unable to save changes. " +
- "Try again, and if the problem persists " +
- "see your system administrator.");
- }
- return View(listingProject);
- }
Advertisement
Add Comment
Please, Sign In to add comment