Suppose I have codes like these
public void Upload (Picture picture)
try
{
//ps is the entity framework
ps.AddToPictures(picture);
ps.SaveChanges();
return picture.PictureId;
}
catch (Exception e) {
//some codes to bound the exception to the model
}
How can I present the exceptions to the model and present them in the view?
Use ModelState.AddError.
Example:
Then in the View:
Can’t remember the correct overload for ValidationMessage – so take a look at the different overloads.
I’d recommend using custom exceptions though – you don’t want to display things like “Null reference exception” in your View.
More on ModelState.AddError here.