C
C#2w ago
morry329#

✅ always gets the invalid data received

I have been creating a simple CRUD app with the ASP.NET. My update function does not work as expected: the save button triggers the error message "invalid data received" ; somehow the model reference is considred null on clicking the save button

//clicking the save btn prompts this method to work
public async Task<IActionResult> Update(ListingProjects updatedUserModelRef)
{
if (updatedUserModelRef == null || updatedUserModelRef.ListingName == null)
{
if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
return Json(new { success = false, message = "Invalid data received." }); //however it always falls into this invalid message..
}
return NotFound();
}

//clicking the save btn prompts this method to work
public async Task<IActionResult> Update(ListingProjects updatedUserModelRef)
{
if (updatedUserModelRef == null || updatedUserModelRef.ListingName == null)
{
if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
return Json(new { success = false, message = "Invalid data received." }); //however it always falls into this invalid message..
}
return NotFound();
}
could anyone kindly advice me on why the save button does not save the user input as expected? the full code here https://pastebin.com/4C2aYF78
Pastebin
/* dashboard view with edit button */ - 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.
1 Reply
Mayor McCheese
$.ajax({
type: "POST",
url: "@Url.Action("Update", "Home")",
data: {
ListingId: id,
ListingName_DTO: newName,
__RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val() // Anti-forgery token
},
$.ajax({
type: "POST",
url: "@Url.Action("Update", "Home")",
data: {
ListingId: id,
ListingName_DTO: newName,
__RequestVerificationToken: $('input[name="__RequestVerificationToken"]').val() // Anti-forgery token
},
if (updatedUserModelRef == null || updatedUserModelRef.ListingName == null)
if (updatedUserModelRef == null || updatedUserModelRef.ListingName == null)
You have no ListingName, it's ListingName_DTO in js.
Want results from more Discord servers?
Add your server