Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a strongly-named way to remove ModelState errors in ASP.NET MVC

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");
like image 516
Simon_Weaver Avatar asked Dec 23 '12 04:12

Simon_Weaver


1 Answers

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);
 }
like image 103
Simon_Weaver Avatar answered Sep 20 '22 12:09

Simon_Weaver