I have a controller action where I perform some validations. I want to use ModelState.AddModelError to summarize the errors and display all at once, but without creating a view and using return content() instead. Is this possible? Here is my code so far:
public ActionResult Validate(string lName, string fName)
{
var lName= DataContext.LName(lName);
var fName = DataContext.FName(fName);
if (lName == null)
ModelState.AddModelError("lName", "Last Name " + lName + " not found);
if (fName== null)
ModelState.AddModelError("fName", "First Name " + fName + " not found);
return Content("Display Error Summary");
}
You are adding the errors to MdoelState but you may need to test the validity also like so:
And then in your Razor veiw