I'm getting code first approach validation error can you help me

Harun Ergün 260 Reputation points
2023-01-31T13:51:55.46+00:00

User's image

User's image

I add error massege and I update database with (datanase-update -Force).but not working error massege. why ?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,237 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,231 Reputation points Microsoft Vendor
    2023-02-01T06:48:35.2033333+00:00

    Hi @Harun Ergün,

    You want to implement model validation, do you use the ValidationMessageFor attribute in the View?

    @Html.ValidationMessageFor(model => model.StudentName, "", new { @class = "text-danger" })

    You can refer to the following examples to see where your code is missing.

    User's image

    public class HomeController : Controller
        {
            
            private SchoolContext db = new SchoolContext();
            public ActionResult Index()
            {           
                return View(db.Students.ToList());
            }
            public ActionResult Edit(int Id)
            {         
                var std = db.Students.ToList().Where(s => s.StudentId == Id).FirstOrDefault();
                return View(std);
            }
            [HttpPost]
            public ActionResult Edit(Student std)
            {           
                var student = db.Students.ToList().Where(s => s.StudentId == std.StudentId).FirstOrDefault();
                student.StudentName = std.StudentName;
                db.SaveChanges();
    
                return RedirectToAction("Index");
            }
        }
    
     public class Student
        {
            public int StudentId { get; set; }
            [Column(TypeName = "Varchar")]
            [StringLength(30, ErrorMessage = "error")]
            public string StudentName { get; set; }
            public int Age { get; set; }
        }
    

    op7

    You can also iterate over each error in the Catch loop to find out where the error is. code show as below:

    try
    {
        db.SaveChanges();
    }
    catch (DbEntityValidationException ex)
    {
        foreach (var entityValidationErrors in ex.EntityValidationErrors)
        {
            foreach (var validationError in entityValidationErrors.ValidationErrors)
            {
                Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
            }
        }
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful