0

I have a model that includes the following property:

[Required]
public string City { get; set; }

Basically, a user selects a stored value from a dropdown. The fields in my partial view are populated with the corresponding data. In certain circumstances, the stored data will not have a value for City. I am trying to use jQuery to check that if the field is empty, to basically not require the field and to allow submission. I have tried the following:

$("#City").attr("data-val", "false");
$("#City").rules("remove", "required");
$("#City").attr("disabled", "disabled");
$("#City").attr("aria-required", "false");
$("#City").removeAttr("required");

None of these work. On submit, the proper method is hit in the controller with a ModelState.IsValid of false and an error stating that "The City field is required."

How can I use jQuery to prevent this field from being validated on submit?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Cameron Gray
  • 57
  • 1
  • 9
  • 1
    This has nothing to do with jQuery (or at least, the jQuery part -- `$("#City").removeAttr("required");` -- is correct). It's an ASP.Net MVC thing. Removing it client-side doesn't tell the *server* that it's no longer required. – T.J. Crowder Oct 03 '17 at 14:18
  • Have a look at [this](https://stackoverflow.com/questions/2417113/asp-net-mvc-conditional-validation) or [this..](https://stackoverflow.com/questions/8242847/model-validation-asp-net-mvc-3-conditional-required-attribute) – palaѕн Oct 03 '17 at 14:21
  • Thanks for the suggestions and T.J thank you for pointing out that this is server side validation. I am still learning validation and had convinced myself this was happening client-side due to how the page was responding. I will reevaluate whether the [Required] attribute is needed on the property in the first place. – Cameron Gray Oct 03 '17 at 14:38
  • Why not check in your controller if city has no value, and if so set it to an empty string before checking if model state is valid. – Abdoulie Kassama Oct 03 '17 at 15:27
  • Use a [foolproof](http://foolproof.codeplex.com/) or similar `[RequiredIf]` conditional validation attribute so you get both client and server side validation. Refer also [The Complete Guide To Validation In ASP.NET MVC 3 - Part 2](https://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) –  Oct 03 '17 at 21:32
  • Hey Stephen, thanks for the suggestion. This is a fairly extensive application so I cannot install new libraries without a fairly extensive testing period. That is however something I would look into using for my personal projects. – Cameron Gray Oct 04 '17 at 13:50
  • Then just write your own validation attribute –  Oct 05 '17 at 01:53

2 Answers2

0

Before checking whether the ModelState.IsValid, you can remove the errors on that property of the Model ModelState["City"].Errors.Clear(); Therefore meaning regardless of what errors have occurred on your City property, the model state will be valid.

Note, this fix should be applied in the controller on the server - it is not related to jQuery.

Michael Hancock
  • 2,673
  • 1
  • 18
  • 37
  • 1
    This is a bad idea because it breaks the separation of concerns on what the model should do and what the controller does. A better solution to this would be to remove the required attribute form the property and implement IValidateObject to your view model. in you implementation you can then check your business rules and emit an error. This also seamlessly works mvc model binding, so that you don't have to hack at the ModelState errors. – Fran Oct 03 '17 at 14:31
  • @Fran I agree. That would certainly be better practice. My answer would just be the minimum required to get O.P. past the Model.IsValid – Michael Hancock Oct 03 '17 at 14:35
  • Thanks for the suggestion. I think I am going to re-evaluate if these properties really need to be set to [Required] at all, given that there are certain situations where they won't be populated. – Cameron Gray Oct 03 '17 at 14:37
0

Even though there were some great suggestions from posters, I ended up reevaluating our needs and found that the Required attribute was not required for that specific property.

Thanks for your suggestions everyone.

Cameron Gray
  • 57
  • 1
  • 9