Is there a way I can override the default validation error that is thrown up for a model property from the controller? For example, the car.make cannot be null, but I want to throw a specific error if the person spells the name of the car make wrong.:
MODEL
public class Car
{
public int ID { get; set; }
[Required]
public string Make { get; set; }
}
VIEW
<div class="form-group">
@Html.EditorFor(model => model.Make, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Make, "", new { @class = "text-danger" })
</div>
CONTROLLER
public ActionResult Create([Bind(Include = "Make,Model")] Car car)
{
ModelState.AddModelError("Car.Make", "Check your spelling");
return View(car);
}
.AddModelError("Car.Make", ...)its.AddModelError("Make", ...)- you model does not contain a property namedCar. But if you only want to select from valid values, consider using a<select>element (or a jQuery autocomplete)