-1

I have below model class. that is shared by multiple views.

Now, For the view1.cshtml both validation attribute(i.e RegularExpression,Required) working fine so I don't need to do anything for that.

Problem is when I submit my second view2.cshtml I want to remove Required validation attribute but not RegularExpression.

Model class has below property

[RegularExpression(@"[A-Za-z]{5}\d{4}[A-Za-z]{1}", ErrorMessage = "Invalid")]
[Required(ErrorMessage = "The PAN no field is required")]
public string pan { get; set; }

I tried below code to remove Required validation but it also remove RegularExpression validation.

ModelState.Remove("pan");
if (ModelState.IsValid)
{

}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mannan Bahelim
  • 1,289
  • 1
  • 11
  • 31
  • I believe **CustomAttribute** could help you sort your problem out. – Skrface May 22 '19 at 12:52
  • @Skrface Is there any reference for CustomeAttribute for this situation. – Mannan Bahelim May 22 '19 at 12:57
  • 1
    doesnt really address what youre asking directly but its thought as best practice to have a separate vm for each view. this helps prevent stuff like overposting/underposting attacks and makes the situation youre trying to address into a trivial problem. What's more clear? two viewmodels or one viewmodel that has a custom written data attribute to conditionally remove validation on one of the vm properties – GregH May 22 '19 at 12:59
  • Actually my VM is shared by multiple views. for this single property validation I have to create multiple copie's of the same VM for each view. this is the only reason to use many view to single model – Mannan Bahelim May 22 '19 at 13:04
  • 1
    @MannanBahelim it's still a good idea to separate them. If all of the views sharing this vm use ALL of the properties in the vm, then sure, go ahead and keep them all using the same vm (although I'm guessing some of your views use subsets of properties from this vm) until one needs to be different. This sounds like the first time one needs to be different and warrants a separate view model being created. – GregH May 22 '19 at 13:07
  • @MannanBahelim you can also use **$("#pan").rules("remove", "required");** using jQuery – Kiran Joshi May 22 '19 at 13:10
  • @KiranJoshi I guess it is ok for client side validation.what about server side – Mannan Bahelim May 22 '19 at 13:13
  • @GregH thanks ,let me try with separate vm. – Mannan Bahelim May 22 '19 at 13:14
  • You can check and add error dynamically using AddModelError in the controller-action and remove the validation from the attribute. – Jeeva J May 22 '19 at 14:50
  • @JeevaJsb please read my question carefully.I don't want to add error in my model. just want to remove only one Required attribute not regularexpression – Mannan Bahelim May 23 '19 at 05:43
  • I meant the same.. Remove required attribute. In controller action, based on your condition add model state error before you validate ModalState.IsValid. So that will satisfy your condition.. – Jeeva J May 23 '19 at 08:01

1 Answers1

-1

Try something like this:

 ModelState["pan"].Errors.Clear();
A.M. Patel
  • 334
  • 2
  • 9
  • I don't want to remove the errors from field. If i add this statement how could I check entered value has passed the regular expression – Mannan Bahelim May 22 '19 at 13:00