Skip to content

Instantly share code, notes, and snippets.

@codereflection
Created October 9, 2012 15:34
Show Gist options
  • Save codereflection/3859562 to your computer and use it in GitHub Desktop.
Save codereflection/3859562 to your computer and use it in GitHub Desktop.
Using ModelState.AddModelError for exception reporting
[HttpPost]
public ActionResult Edit(DonorViewModel model)
{
try
{
var donor = db.Donors.Get(model.Donor_ID);
model.CreatedOn = donor.CreatedOn;
model.DonorType_ID = donor.DonorType_ID;
model.ModifiedOn = DateTime.Now;
SetupEditViewData(model);
db.Donors.Update(model);
return RedirectToAction("Index");
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
return View(model);
}
}
@skoon
Copy link

skoon commented Oct 11, 2012

Well, two questions:

  1. What do you expect the users to do with the Model Error?

  2. Do you still display the model even if there are errors?

@codereflection
Copy link
Author

The only thing the users can do is report the error.

I would still display the model values on error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment