2

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...

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

i need a way to handle the jQuery validation. The model properties have required field validators on them. On click of the 'Save Draft' button, I would like to ignore validation of certain fields. I cannot change the Model as it is being used in other places too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.