好吧,一個瘋狂的需求需要一個瘋狂的解決方案!在我的控制,我用 if(this.ModelState.IsValid)...
,但現在我可以用 if(ValidatorChecker<ModelType>.IsModelStateValid(this.ModelState))...
,
有以下幾點:
internal static class ValidatorChecker<TModel>
{
public static bool IsModelStateValid(ModelStateDictionary modelState)
{
if (modelState.IsValid)
{
return true;
}
int totalErrors = 0, requiredAttributeErrors = 0;
Type modelType = typeof(TModel);
foreach (var key in modelState.Keys)
{
if (modelState[key].Errors.Count > 0)
{
totalErrors += modelState[key].Errors.Count;
Type currentType = modelType;
string[] typeParts = key.Split(
new char[] { '.' },
StringSplitOptions.RemoveEmptyEntries);
int typeIndex = 0;
if (typeParts.Length == 0)
{
continue;
}
else if (typeParts.Length > 1)
{
for (typeIndex = 0; typeIndex < typeParts.Length - 1; typeIndex++)
{
currentType =
currentType.GetProperty(typeParts[typeIndex]).PropertyType;
}
}
PropertyInfo validatedProperty =
currentType.GetProperty(typeParts[typeIndex]);
var requiredValidators =
validatedProperty.GetCustomAttributes(typeof(RequiredAttribute), true);
requiredAttributeErrors += requiredValidators.Length;
}
}
return requiredAttributeErrors == totalErrors;
}
}