Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8517087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:36:18+00:00 2026-06-11T05:36:18+00:00

[HttpPost] public ActionResult Edit(Car car) { if (ModelState.IsValid) { db.Entry(car).State = EntityState.Modified; db.SaveChanges(); return

  • 0
[HttpPost]
        public ActionResult Edit(Car car)
        {
            if (ModelState.IsValid)
            {
                db.Entry(car).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(car);
        }

This is a controller method scaffolded by MCV 4
My “car” entity has a unique field: LicensePlate.
I have custom validation on my Entity:

Validation:

public partial class Car
    {
        partial void ValidateObject(ref List<ValidationResult> validationResults)
        {
            using (var db = new GarageIncEntities())
            {
                if (db.Cars.Any(c => c.LicensePlate.Equals(this.LicensePlate)))
                {
                    validationResults.Add(
                        new ValidationResult("This licenseplate already exists.", new string[]{"LicensePlate"}));
                }
            }
        }
    }

should it be usefull, my car entity:

 public partial class Car:IValidatableObject
    {
        public int Id { get; set; }
        public string Color { get; set; }
        public int Weight { get; set; }
        public decimal Price { get; set; }
        public string LicensePlate { get; set; }
        public System.DateTime DateOfSale { get; set; }
        public int Type_Id { get; set; }
        public int Fuel_Id { get; set; }

        public virtual CarType Type { get; set; }
        public virtual Fuel Fuel { get; set; }



        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
            {
                var result = new List<ValidationResult>();
                ValidateObject(ref result);
                return result;
            }

        partial void ValidateObject(ref List<ValidationResult> validationResults);
    }

QUESTION:

Everytime I edit a car, it raises an error:

Validation failed for one or more entities. See
‘EntityValidationErrors’ property for more details.

The error is the one raised by my validation, saying it can’t edit because there is already a car with that license plate.

If anyone could point me in the right direction to fix this, that would be great!
I searched but couldn’t find anything, so even related posts are welcome!

note: this field has a unique constraint, so it is imperative that this validation is still triggered for a create-action

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:36:19+00:00Added an answer on June 11, 2026 at 5:36 am

    Allright I found a fix but I’m not sure it’s the best ever.
    I modified the validation so that it only triggers when the Id is non existend (so.. 0).
    This way, I can make a distiction between new entities and updated ones.

    if (db.Cars.Any(c => c.LicensePlate.Equals(this.LicensePlate) && c.Id != this.Id))
    

    This does fix my problem, but somehow I think there should be a cleaner fix.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Controller First I tried this: [HttpPost] public ActionResult Edit(JournalEntry journalentry) { if (ModelState.IsValid) {
This works: [HttpPost] public ActionResult Slots(Slots slots, ICollection<int> jobNos) { if (!ModelState.IsValid) return View(new
i have 2 actions public ActionResult FilesAdd(int id) { FillParentMenuDDL(id); return View(); } [HttpPost]
I have the following Edit method: [HttpPost] public ActionResult Edit(Movie movie) { try {
Code below: [HttpGet] public ActionResult Edit(string id=) { // ... } [HttpPost] public ActionResult
I have an [HttpPost] action method signature like this: [HttpPost] public ActionResult Edit(ExistingPostViewModel model)
when updating ViewModels via [HttpPost] public ActionResult Edit(int id, AddressChooserEnum addressChooser, string btnSubmit, FormCollection
Controller public ActionResult Create() { return View(new PageViewModel()); } [HttpPost] public ActionResult Create(Page page)
I have this function in my controller. [HttpPost] public ActionResult Edit(EmployeesViewModel viewModel) { Employee
I've got an Edit action like this: [HttpPost] public ActionResult Edit(UserModel user1) { if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.