Last active
October 25, 2017 14:03
-
-
Save dontpaniclabsgists/3c0ecd646757b48bab3d98ad3fd0fa4a to your computer and use it in GitHub Desktop.
validating_fields_mvc_1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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