C
C#•3w ago
morry329#

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"
`mysql> SELECT * FROM ListingDTO_DBTable;
+----+---------------------+
| Id | ListingName |
+----+---------------------+
| 3 | palace of hormisdas |. <----- this is super strange! at Id 3 the ListingName has not been edited at all!
| 36 | NULL | <----- instead a new entry has been created with a new Id and a new NULL value! the user input has not been saved at all
+----+---------------------+
2 rows in set (0.00 sec)
`mysql> SELECT * FROM ListingDTO_DBTable;
+----+---------------------+
| Id | ListingName |
+----+---------------------+
| 3 | palace of hormisdas |. <----- this is super strange! at Id 3 the ListingName has not been edited at all!
| 36 | NULL | <----- instead a new entry has been created with a new Id and a new NULL value! the user input has not been saved at all
+----+---------------------+
2 rows in set (0.00 sec)
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
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.
No description
16 Replies
Angius
Angius•3w ago
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?
Sehra
Sehra•3w ago
ListingDTO_DBTable.ListingId= $("#EditIdButHidden").val(); change that to ListingDTO_DBTable.Id
ACiDCA7
ACiDCA7•3w ago
also maybe this from docs relevant
ACiDCA7
ACiDCA7•3w ago
No description
Sehra
Sehra•3w ago
also when posting { ListingDTO_DBTable: ListingDTO_DBTable } would make a nested object, just send ListingDTO_DBTable as is
morry329#
morry329#OP•3w ago
So 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=)?
Sehra
Sehra•3w ago
see https://api.jquery.com/jQuery.post/, it's takes a string or PlainObject as data
morry329#
morry329#OP•3w ago
Ahh I see
No description
morry329#
morry329#OP•3w ago
So if that .Update() method is custom-made the DbSet could be updated, did I understand that correctly?
Sehra
Sehra•3w ago
think it should work as it is, give it a try. normally you don't name database entities DTO
morry329#
morry329#OP•3w ago
so the problem still persists unfortunately. it definitely lies in the backend like db
Sehra
Sehra•3w ago
set a breakpoint and make sure you get expected data in EditListingJ
morry329#
morry329#OP•3w ago
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 before
Sehra
Sehra•3w ago
try with [FromBody] instead since you are not submitting form data
morry329#
morry329#OP•3w ago
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 error
Sehra
Sehra•3w ago
ok, 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

Did you find this page helpful?