I feel like starting to bang my head against my keyboard after many months of Googling and posting in other forums. So I really appreciate any insights.
My problem is that as mentioned in the title the edit http POST
method does not save user inputs once the save button is clicked on browser. This video illustrates my point: https://imgur.com/a/GWBHXp1
I tried debugging my code here:
[HttpGet]
public JsonResult EditListingJ(int Id) //For getting details of the selected User
{
try
{
var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id);
if (ListingDTO_DBTable != null)
{
Console.WriteLine($"Got it");
return Json(ListingDTO_DBTable);
}
return Json(null);
}
catch (Exception ex)
{
Console.WriteLine("so null nothing can be edited");
return null;
}
}
[HttpPost]
public JsonResult EditListingJ(int Id, ListingProjectsDTO _listingProjectsDto) //For Updating changes
{
try
{
var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id); //fetching the user with Id
if (ListingDTO_DBTable != null)
{
_context.ListingDTO_DBTable.Update(ListingDTO_DBTable); //deleting from db
_context.SaveChanges();
Console.WriteLine("saving finally success");
return Json(ListingDTO_DBTable);
}
}
catch (Exception ex)
{
Console.WriteLine("saving failed");
return null;
}
Console.WriteLine("saving failed too");
return Json(_listingProjectsDto);
}
And it gave me this output
Console.WriteLine("saving failed too")
which means my edit has not been saved anywhere. A little more digging found out that
var ListingDTO_DBTable = _context.ListingDTO_DBTable.Find(Id);
returns null.
Could anyone kindly point out me in the right direction? No idea why those lines went to null etc.
As the documentation of the Find
method states,
"If no entity is found, then null is returned"
The most likely cause is that that the Id
value passed to Find
corresponds to no row in the database.