Skip to content

Instantly share code, notes, and snippets.

@dontpaniclabsgists
Last active October 25, 2017 14:03
Show Gist options
  • Save dontpaniclabsgists/3c0ecd646757b48bab3d98ad3fd0fa4a to your computer and use it in GitHub Desktop.
Save dontpaniclabsgists/3c0ecd646757b48bab3d98ad3fd0fa4a to your computer and use it in GitHub Desktop.
validating_fields_mvc_1
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MyModel myModel)
{
// Manually validate model in every action
if(myModel.Item3 < myModel.Item1 + myModel.Item2)
{
ModelState.AddModelError("Item3", "Item1 + Item2 must be less than Item3");
return View(myModel);
}
if (ModelState.IsValid)
{
db.MyModels.Add(myModel);
db.SaveChanges();
return RedirectToAction(nameof(Index));
}
return View(myModel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment