views:

37

answers:

1

Hi all,

I have a customized ModelBinder which bind web from with a object using code like this"

    [ModelBinder(typeof(CustomizedModelBinder))]
    public class Widget{ ... }

This modelbinder may throw exceptions and where should I add code to catch those exceptions? Thanks in advance!

+2  A: 

From design perspective it is better for a model binder to add model errors instead of throwing exceptions:

ModelState.AddModelError("Phone", "Phone number is invalid.");

This way, later in your action you could check if the model is valid:

if (!ModelState.IsValid)
{
    ...
}
Darin Dimitrov
Thanks! But I still would like to keep on view page which will be redirected after a exception is catched.
Roy