2

I have a 3 steps forms where I bind for each step on a ViewModel that has DataAnnotation. Everything works fine except that when I click on my "previous" button, the validation gets called and, if there's missing values, the user has to fill all required fields. If I go with an action link, then the data won't be persisted. What I'm looking for here is a way to save my values without calling the validation. I also consider using cookies but I don't think that it's the best way of doing it.

Any ideas or suggestions?

VinnyG
  • 6,883
  • 7
  • 58
  • 76

2 Answers2

1

I use separate models for each step of my wizard pages. I also make sure that the previous clicks do not hit ModelState.IsValid which is what triggers the validation check.

I store the results of each step using session state stored in SQL Server. You can also use hidden variables but I didn't like that solution.

Add a comment if you need more detail or show some code for us to see.

rboarman
  • 8,248
  • 8
  • 57
  • 87
  • Thanks for the reply, that's how I work too my question would be more about how do you make the previous button not to hit ModelState.IsValid? I use client validation, I know that in my controller I just have to not call the Model.IsValid() but I don't know ow to "disable" client validation for my previous button. I can post my code if you need it but I don't thnink it's needed. – VinnyG Dec 13 '10 at 17:12
  • My previous button just calls an action method without submitting the form. You can also detect which button was clicked and then call ModelState.Clear to zero out any errors. – rboarman Dec 13 '10 at 17:21
  • What I try to do is to save the fields when the user click on previous, so I can't do it with an ActionLink. So what I want is to submit the form but it's not working since my required fields popup. – VinnyG Dec 13 '10 at 20:50
1

Ok after lots of searching I found out a solution for having a Wizard like multiple forms with previous and next buttons that won't call the client validation from Data Annotation and MicrosoftMvcValidation.js. I found this post : jQuery Validation plugin: disable validation for specified submit buttons witch simply add a class to the submit button but it was not working for me so I look again and found this one : http://forums.asp.net/p/1622628/4165648.aspx witch has a solution working for me in javascript : document.getElementById("someButton").disableValidation = true; I would prefer a jQuery solution so I could do something with the class attribute of my buttons but it's working for today and I've spent too mutch time on this.

Hope it helps some else who's trying to do a "Cancel" button or a Wizard like forms in MVC2 and want's to post the form to the controller so he needs to clear the validation.

Community
  • 1
  • 1
VinnyG
  • 6,883
  • 7
  • 58
  • 76