C
C#2mo ago
morry329#

Insert Method does not insert or save the user input as wanted

I have a working UI with a text field to enter a property name like "Ca* Oro Bridge" on my MVC view. The save button is also displayed on the view. Onclick it does not save the userinput in the text field (henceforth it is also not saved in the DB). I have debugged the follwing code responsible for handling user inputs
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);
}
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);
}
` I am not versed in the world of debugging, but it looks like the HttpGet method refers or returns null. InsertListing method (HttpPost method) deems it as !ModelState.Isvalid (which means my code does not save the input at all) Now I don't quite understand what caused all this null and invalidity. https://pastebin.com/DhCg4Md2 Can anyone kindly point me in the right direction?
Pastebin
/Model/using System.Collections;using Microsoft.Build.Framework;n...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server