✅ MvC | IAction edit is not able to edit data
So I am following an YT tutorial for creating a CRUD operation with ASP.NET.
My tutorial code can create and read data but not update ( or edit). Whenever I tried to edit it pops the custom error message `
It was supposed to be the success message ``
Here's my code, could anyone point me in the right direction?
Controller
https://pastebin.com/fHLMY3jr
Edit.cshtml
https://pastebin.com/EmNXeEZ0
Pastebin
[HttpGet] public IActionResult Edit(int Id) { try { var employ...
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 Mitarbeiter.Models.EmployeeViewModel@{ ViewData["Title"] = "...
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.
25 Replies
Means modelstate is not valid
So some properties from
EmployeeViewModel
fail validationhm ok
So here is my
EmployeeViewModel
I have tried to make some properties here nullable but I am still getting the same old TempData["errorMessage"] = $"Employee not found";
I cannot spot any more errors as I am pretty much new to it
`Add a validation summary to the form to see what the errors are
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-8.0#the-validation-summary-tag-helper
Alright, I have added the validation summary under Edit.cshtml and ran the app again. Here's what I got
No idea where those arror came from. I have a bunch of custom error messages in the Controller like Employee details not available with the Id: {Id}, "Employee with Id {employee.Id} not found". But the validation summary showed totally new errors I have never seen
Well, it means that for some reason the form sends the value of
'Edit'
for the ID
Open the browser devtools and see what data the form actually sendsI hope I am checking the right section of the browser devtools
Seems to me like that the form sends the document called "Edit" on clicking the Submit button as per screenshot
I am not sure what is this Edit referring to. I have a Edit.cshtml file and two Edit methods with the IActionResult data type (in the Controller)
Check the payload tab, not the response
It will show the data that gets sent
Ooh I see, sorry I sent you the wrong screen shot. I am pretty new to this
This would imply the form doesn't send any data
Odd
oh dear
Try sending the form again, and check the new request
Ah, no, wait, data does get sent
Sorry, my bad
Huh... how does your controller class look?
The signature and attributes, specifically
Here you go https://pastebin.com/24QR9Xi6
Pastebin
public class EmployeeController : Controller { privat...
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.
It's basically copy & paste from chatgpt and a YT tutorial
I don't see any attributes on the class
Are you doing routing somewhere else?
Ok please bear me with the stupid question but routing is like [Key] [DatabaseGenerated(DatabaseGenerated(DatabaseGeneratedOption.Identity)]? If so I have these in the data model class
like this
public class Employees
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Column(TypeName = "varchar(50)")]
public string? FirstName { get; set; }
public string? LastName { get; set; }
public DateTime DOB { get; set; }
public string? Email { get; set; }
public double Salary { get; set; }
}
No, this is a database model
And the attributes here configure this database model
I mean this: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-8.0
Ohhh yes
I practice with the C# 6.0 so the Program.cs does the routing
In the last few lines of the Program.cs there is the routing:
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
The version and where routing happens are unrelated
Regardless, this seems... fine. I wonder how the
id
gets the value of Edit
In the meantime chatGPT gave me this idea
I added
<input type="hidden" asp-for="Id" />
Then my app started to let me edit and save the changes like nothing had happened - my problem solved.
Do you have any idea why chatGPT suggested this? 🤔I guess it was filling the ID with garbage data for some reason, and this passes the proper ID instead
I got it. But without you suggesting browser tools and validation summary chatGPT could not come up with this. So thank you so much for your insights 🙏
/solved