Is there a strongly-named way to remove ModelState errors in ASP.NET MVC
If this is for MVC 6, suggest using ModelBindingHelper.ClearValidationStateForModel(Type, ModelStateDictionary, IModelMetadataProvider, string)
.
Here's my solution - a RemoveFor()
extension method on ModelState
, modelled after MVC HTML helpers:
public static void RemoveFor<TModel>(this ModelStateDictionary modelState,
Expression<Func<TModel, object>> expression)
{
string expressionText = ExpressionHelper.GetExpressionText(expression);
foreach (var ms in modelState.ToArray())
{
if (ms.Key.StartsWith(expressionText + "."))
{
modelState.Remove(ms);
}
}
}
Here's how it's used :
if (model.CheckoutModel.ShipToBillingAddress == true)
{
// COPY BILLING ADDRESS --> SHIPPING ADDRESS
ShoppingCart.ShippingAddress = ShoppingCart.BillingAddress;
// REMOVE MODELSTATE ERRORS FOR SHIPPING ADDRESS
ModelState.RemoveFor<SinglePageStoreModel>(x => x.CheckoutModel.ShippingAddress);
}
if (ModelState.IsValid)
{
// should get here now provided billing address is valid
}
If anybody can see a way to improve it (or not have to specify the generic type argument) then please let me know. Or if this exists in MvcFutures under a different name I'd rather switch to that.
While I'm at it here's a helper to check if ModelState is valid for a certain 'tree'
public static bool IsValidFor<TModel, TProperty>(this TModel model,
System.Linq.Expressions.Expression<Func<TModel, TProperty>> expression,
ModelStateDictionary modelState)
{
string name = ExpressionHelper.GetExpressionText(expression);
return modelState.IsValidField(name);
}
Which can be used like this :
if (model.IsValidFor(x => x.CheckoutModel.BillingAddress, ModelState))
{
_debugLogger.Log("Billing Address Valid", () => ShoppingCart.BillingAddress);
}