Created
December 10, 2011 11:45
-
-
Save stormwild/1454973 to your computer and use it in GitHub Desktop.
To get a list of errors in the model state
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] | |
| public ActionResult Edit(ViewModel viewModel) | |
| { | |
| if (ModelState.IsValid) | |
| { | |
| repository.SaveModel(viewModel); | |
| TempData["message"] = "Changes have been saved"; | |
| return RedirectToAction("Index"); | |
| } | |
| else | |
| { | |
| // Referenced from http://stackoverflow.com/questions/5510814/c-sharp-net-mvc3-modelstate-isvalid - Darin Dimitrov | |
| ViewBag.Errors = ModelState | |
| .Where(x => x.Value.Errors.Count > 0) | |
| .Select(x => new { x.Key, x.Value.Errors }) | |
| .ToArray(); | |
| return View(viewModel); | |
| } | |
| } |
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
| @if (ViewBag.Errors != null) | |
| { | |
| foreach (var e in ViewBag.Errors) | |
| { | |
| <p>@e</p> | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment