2
Vote

Allow to show model-level error(s)

description

The following is just a little experience to share.
 
Sometimes I want to show model-level error(s), such as a database error, and because the error(s) is/are not related with any property, so maybe it is good to show the error(s) in the summary.
 
But I found no such a function in the GREATE xVal ( I just started to use it for 1 day).
 
So I made a little change in the xVal -> ServerSide -> RulesException.cs -> public void AddModelStateErrors(ModelStateDictionary modelState, string prefix, Func<ErrorInfo, bool> errorFilter)
 
-------------↓-----------------
 
var key = prefix + errorInfo.PropertyName;
//the following line is the original code.
//modelState.AddModelError(key, errorInfo.ErrorMessage);
 
//changed by C.T., so that can add a model-level error
//2010-01-22

region model-level error

 
if (string.Equals(errorInfo.PropertyName, "ModelLevelError", StringComparison.OrdinalIgnoreCase))
modelState.AddModelError(string.Empty, errorInfo.ErrorMessage);
else
modelState.AddModelError(key, errorInfo.ErrorMessage);
 

endregion

 
-------------↑-----------------
 
I use it like the following:
 
***BLL
 
// I added a new method GetDisplayName to class DataAnnotationsValidationRunner, so I can use the property name DRYly.
//public static string GetDisplayName(object instance, string propertyName)
//{
// return TypeDescriptor.GetProperties(instance).Find(propertyName, true).DisplayName;
//}
 
-------------↓-----------------
public static int Create(Entry entry)
{
int ret = 0;
 
string duplicateEntryIdErrorMessage = string.Format("The {0} exists, please input another {0}.", DataAnnotationsValidationRunner.GetDisplayName(entry, "entryId"));
 
if (CheckEntryId(entry.EntryId))
throw new RulesException("EntryId", duplicateEntryIdErrorMessage, entry);
 
var errors = DataAnnotationsValidationRunner.GetErrors(entry);
if (errors.Any())
throw new RulesException(errors);
 
try
{
ret = EntryDB.Create(entry);
}
catch
{
throw new RulesException("ModelLevelError", "a dababase error occurs.");
}
 
if (ret == -1)
throw new RulesException("ModelLevelError", "something wrong when create a new entry.");
 
return ret;
}
-------------↑-----------------
 
*** ViewModel
-------------↓-----------------
...
...
-------------↑-----------------
 
*** controller / action
// nothing special just like the xVal's guide
-------------↓-----------------
[HttpPost]
public ActionResult EntryNew(Entry entry)
{
ViewData["BodyClass"] = "page-entryNew";
 
try
{
EntryManager.Create(entry);
}
catch(RulesException ex)
{
ex.AddModelStateErrors(ModelState, "entry");
}
 
return ModelState.IsValid ? RedirectToAction("EntryNewSuccess", "Dictionary") : (ActionResult)View(new EntryNewViewModel());
}
-------------↑-----------------
 
*** views
//The first parameter in Html.ValidationSummary is "true", I want to show model-level error(s) here.
//But if there is/are no model-level error(s) and there is/are property error(s), the summary always show :(
-------------↓-----------------
...
<%= Html.ValidationSummary(true, "Please check error(s).") %>
...
-------------↑-----------------

comments