The edit method does not edit the pre-existing data but it creates a new data entry with NULL
I am super confused with how unexpectedly my ASP.NET app behaves which was supposed to be a simple CRUD. My app has a huge problem with editing the pre-existing data row via frontend. I totally appreciate a second set of eyes.
Please see the attached video https://imgur.com/a/nh6GDih - the app shows the empty text field once the Edit button is clicked. Once the save button is clicked I got the message user edited as per screenshot attached.
However, in my DB my user input has been saved as a new entry instead of edit for "palace of hormisdas"
I wanted to see that the entry with Id 3 has the edited ListingName (not "palace of hormisdas" anymore but whatever value I enter via Edit UI
Could anyone kindly advise me on what went wrong here?
You can find my frontend code here https://pastebin.com/SM3nhsHr and backend code here https://pastebin.com/3c7Gz4gX
I created a separate pastebin link for my Program.cs (not sure it's relevant to this problem though) https://pastebin.com/2agWg75A
Imgur
Untitled Album
Pastebin
@model IEnumerable@{ ViewData["Title"] = "Dashboard"; - Pastebin.com
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.
Pastebin
/* Model */public class ListingProjectsDTO { [System.Compone...
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.
Pastebin
using System.Net;using System.Text.Json.Serialization;using Microso...
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.

16 Replies
You can't update a
DbSet<Thing>
with a ThingDto
Honestly, weird it would even let you do that without error
Unless that .Update()
method is something custom
Or, wait
You have a table of DTOs?
Whuh?ListingDTO_DBTable.ListingId= $("#EditIdButHidden").val();
change that to ListingDTO_DBTable.Id
also maybe this from docs relevant

also when posting
{ ListingDTO_DBTable: ListingDTO_DBTable }
would make a nested object, just send ListingDTO_DBTable
as isSo my entity is in the added state as it has no primary key set (henceforth my app adds a new entry instead of editing the pre-existing one). Did I understand the doc correctly?
Ahhhh you got an amazing set of eyes .. Thank you so much for pointing that out, could not notice it myself
A stupid question maybe, but how could ListingDTO_DBTable be sent as it is? (Also without creating a nested object=)?
Ahh I see

So if that .Update() method is custom-made the DbSet could be updated, did I understand that correctly?
think it should work as it is, give it a try. normally you don't name database entities DTO
so the problem still persists unfortunately. it definitely lies in the backend like db
set a breakpoint and make sure you get expected data in EditListingJ
So my rider does not let me set any breakpoint in code (the point will get marked by 🚫 ), I did something like this as debug
[HttpGet]
public JsonResult EditListingJ(int? Id) //For getting details of the selected User
{
try
{
ListingProjectsDTO StartEditListing = _context.ListingDTO_DBTable.Find(Id);
Console.WriteLine("GET success");
return Json(StartEditListing);
}
catch (Exception e)
{
return null;
}
}
[HttpPost]
public JsonResult EditListingJ([FromForm]ListingProjectsDTO _completeEditNowListingProjectsDto)
{
try
{
_context.ListingDTO_DBTable.Update(_completeEditNowListingProjectsDto);
_context.SaveChanges();
Console.WriteLine("POST success");
return Json(_completeEditNowListingProjectsDto);
}
catch (Exception ex)
{
return Json(ex.Message);
}
}
This code returns both of Console.WriteLine("GET success") and Console.WriteLine("POST success");
Still, the edited input will not get saved and the DB gets the NULL entry just like beforetry with [FromBody] instead since you are not submitting form data
With FromBody I always get
the server responded with a status of 415 (Unsupported Media Type)
That's why I went with FromForm to get rid of this errorok, maybe should be FromForm then. most uses i've seen is $.ajax instead of $.post
but either way, get debugging working cause doing console.writeline is the wrong way to go about it