C
C#11mo ago
morry329#

✅ 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
Employee not found
Employee not found
` It was supposed to be the success message
"Employee edited successfully!"
"Employee edited successfully!"
`` 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
Angius
Angius11mo ago
Means modelstate is not valid So some properties from EmployeeViewModel fail validation
morry329#
morry329#OP11mo ago
hm 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
using System.ComponentModel;
using System.Linq;

namespace Mitarbeiter.Models
{
public class EmployeeViewModel
{
public int Id { get; set; }

[DisplayName("First Name")]

public string? FirstName { get; set; }

[DisplayName("Last Name")]

public string? LastName { get; set; }

[DisplayName("Date of Birth")]

public DateTime DOB { get; set; }

[DisplayName("Email")]

public string? Email { get; set; }

public double Salary { get; set; }

[DisplayName("Name")]

public string FullName {
get { return FirstName + " " + LastName; }
}

}
}
using System.ComponentModel;
using System.Linq;

namespace Mitarbeiter.Models
{
public class EmployeeViewModel
{
public int Id { get; set; }

[DisplayName("First Name")]

public string? FirstName { get; set; }

[DisplayName("Last Name")]

public string? LastName { get; set; }

[DisplayName("Date of Birth")]

public DateTime DOB { get; set; }

[DisplayName("Email")]

public string? Email { get; set; }

public double Salary { get; set; }

[DisplayName("Name")]

public string FullName {
get { return FirstName + " " + LastName; }
}

}
}
`
Angius
Angius11mo ago
Add a validation summary to the form to see what the errors are
<div asp-validation-summary="All"></div>
<div asp-validation-summary="All"></div>
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-8.0#the-validation-summary-tag-helper
morry329#
morry329#OP11mo ago
Alright, I have added the validation summary under Edit.cshtml and ran the app again. Here's what I got
No description
morry329#
morry329#OP11mo ago
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
Angius
Angius11mo ago
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 sends
morry329#
morry329#OP11mo ago
I 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)
No description
Angius
Angius11mo ago
Check the payload tab, not the response It will show the data that gets sent
morry329#
morry329#OP11mo ago
Ooh I see, sorry I sent you the wrong screen shot. I am pretty new to this
No description
Angius
Angius11mo ago
This would imply the form doesn't send any data Odd
morry329#
morry329#OP11mo ago
oh dear
Angius
Angius11mo ago
Try sending the form again, and check the new request Ah, no, wait, data does get sent Sorry, my bad
Angius
Angius11mo ago
No description
Angius
Angius11mo ago
Huh... how does your controller class look? The signature and attributes, specifically
morry329#
morry329#OP11mo ago
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.
morry329#
morry329#OP11mo ago
It's basically copy & paste from chatgpt and a YT tutorial
Angius
Angius11mo ago
I don't see any attributes on the class Are you doing routing somewhere else?
morry329#
morry329#OP11mo ago
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; } }
Angius
Angius11mo ago
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
morry329#
morry329#OP11mo ago
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?}");
Angius
Angius11mo ago
The version and where routing happens are unrelated Regardless, this seems... fine. I wonder how the id gets the value of Edit
morry329#
morry329#OP11mo ago
In the meantime chatGPT gave me this idea
No description
morry329#
morry329#OP11mo ago
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? 🤔
Angius
Angius11mo ago
I guess it was filling the ID with garbage data for some reason, and this passes the proper ID instead
morry329#
morry329#OP11mo ago
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
Want results from more Discord servers?
Add your server