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);
}
}
@bm2yogi
Copy link

bm2yogi commented Oct 9, 2012

I see your point. Would any of these work?

  1. Add a generic message to the ModelState Errors and optionally log the exception.
  2. Map the few errors you expect to more user friendly messages (maybe with a simple hashtable).

Or, you could just leave it as "good enough." If it's an internal app and your users will get the point, then I don't see anything wrong with it in this case.

@codereflection
Copy link
Author

Thanks for the response. :) I think I'll probably go with your first suggestion. The trouble is this is not an internal app, but rather used by PTA parents who can have the entire spectrum of computer knowledge.

@bm2yogi
Copy link

bm2yogi commented Oct 9, 2012

Oh, In that case, you should just have the ValidationSummary display your cell number and email address. :P

@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