Is there a way to remove ModelState errors during an ASP.NET MVC postback without having to write each one by hand.
Let's say we have a checkbox Billing Same As Shipping
and we want to then ignore anything user wrote for ShippingAddress
when it's checked - typically what you might do is this.
ModelState.Remove("Checkout.ShipppingAddress.FirstName");
ModelState.Remove("Checkout.ShipppingAddress.LastName");
ModelState.Remove("Checkout.ShipppingAddress.Address1");
ModelState.Remove("Checkout.ShipppingAddress.Address2");
...
ModelState.Remove("Checkout.ShipppingAddress.ZipCode");
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With