Is there any way to ignore some properties (on a POCO) when validating a form in ASP.NET MVC3?

前端 未结 9 593
春和景丽
春和景丽 2021-02-05 05:59

i\'ve got a sign up wizard for new user registration. When I try to goto the 2nd page, I get validation errors because my User object hasn\'t been fully populated,

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-05 06:53

    This is discussed in Steve Sanderson's asp.net mvc 2 book, page 486.

    Create a a custom attribute, ValidateIncomingValuesOnlyAttribute, that inherits from ActionFilterAttribute, and apply it to your controller class.

    Override the OnActionExecuting method:

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
    
    var modelState = filterContext.Controller.ViewData.ModelState;
    var incomingValues = filterContext.Controller.ValueProvider;
    
    var keys = modelState.Keys.Where(x => !incomingValues.ContainsPrefix(x));
    foreach(var key in keys)
    {
    modelState[key].Errors.Clear();
    }
    }
    

    That way you validate the data the pertains to each step in the wizard only. You then need a confirmation page with no data input to send off the validated data to the server.

    But most of all, read Steve Sanderson's book, it gives a working solution to this and your other problem.

    Addendum:

    If instead of the above you decide to map to a viewmodel, be careful because you will either have to:

    a. Not decorate the viewmodel properties with validation dataannotation attributes, in which case you only validate once the user has filled in the entire wizard and try to submit to the database. This would be very naff from the user's perspective...

    b. Else, you still have to use the technique described by S Sanderson, ie, clear any validation errors that don't pertain to the fields on the current step.

    I don't see the accepted answer as answering the question as it was asked.

提交回复
热议问题