You can create a custom validation summary and add all your errors with a special key (in this example i use _FORM. For example :
private const string VALIDATIONSUMMARY_HMTL = "<div class=\"input-validation-error\">{0}</div>";
public static string ValidationSummary(this HtmlHelper helper, bool customErrorOnly)
{
return ValidationSummary(helper, customErrorOnly, "_FORM");
}
public static string ValidationSummary(this HtmlHelper helper, bool customErrorOnly, string errorName)
{
if (helper.ViewData.ModelState.IsValid)
{
return null;
}
string list = "<ul>";
bool displayList = false;
foreach (KeyValuePair<string, ModelState> pair in helper.ViewData.ModelState)
{
foreach (ModelError error in pair.Value.Errors)
{
if (pair.Key.ToUpper() == "_FORM" || !customErrorOnly)
{
list += "<li>" + error.ErrorMessage + "</li>";
displayList = true;
}
}
}
list += "</ul>";
if (!displayList)
{
return null;
}
return string.Format(VALIDATIONSUMMARY_HMTL, list);
}
When you want to add a specific error :
ViewData.ModelState.AddModelError("_FORM", "My error message");