简体   繁体   中英

MVC3 jQuery - partial validation

I have a form in which a user enters 2 types of data: 1. Project details 2. Survey questions related to the project

The form has 2 submit buttons 1. Save Draft 2. Submit

The 'Save Draft' post needs to validate only project details and not survey fields. The Submit post should validate both project and survey data.

Is there a way to partially validate data when user clicks 'Save Draft'? Maybe handle the 'Save Draft' click and ignore/remove validation for the survey fields...

You can do partial validation by with an action filter attribute. You might be able to customize to your needs. It gives you access to the request and the model state. With access to the model state you can modify the validation errors.

public class ValidateDraftAttribute : ActionFilterAttribute 
{  
  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {

     var modelState = filterContext.Controller.ViewData.ModelState;
     var incomingValues = filterContext.Controller.ValueProvider;

      modelState[key].Errors.Clear();

  }
}

The attribute is then added to the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM