ASP.NET MVC Binding - Duplicated Messages
I'm using ASP.NET MVC Binding Framework.
Let's say I have a class Item, that has a mandatory field Id.
I am binding a List, and do not want one error message for each element in this list.
If more then one error happen when binding the Id field, I want only开发者_StackOverflow one message to be added to ModelState and shown to the user.
In one sentence: Is there a way to avoid duplicated messages using ASP.NET MVC Binding Framework?
You can invent your own solution. For example, your model binder can add model errors as special ModelItemException objects. Then an ActionFilter or base controller's OnActionExecuted() method will walk the ModelState errors, take all the ModelItemException errors there, and merge them into single error. Or you can have such exceptions to implement something like
public interface IItemError
{
public object ItemKey { get; set; }
public string Merge(params Exception[] itemErrors);
}
Feel free to improve this idea further.
精彩评论