0

I am using MVC-View Model, EF Model first

I am having problems with two of my Properties inside my View Model which is following:

[Required]
public string TeamName { get; set; }
[Required]
public string ConsultantName { get; set; }

These two properties can get disabled depending on what SubjectTypeName the user choose

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

Lets say I choose a SubjectTypeName Value and my TeamName and ConsultantName gets disabled, but when I want to save the form I get error beacuse my ModelState.IsValid gets false. And its the [Required] that makes it "false".

Is there any easy solutions for this? Like a easy Jquery code to inactive TeamName and ConsultantName from the validation so my ModelState.IsValid becomes "true"?

Thanks in Advance!

3 Answers 3

1

You may take a look at the following blog post which illustrates how you could write a custom [RequiredIf] validation attribute that will handle conditional validation on the server and on the client. You can directly download the Mvc.ValidationToolkit which contains this validation attribute.

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

5 Comments

Are you no longer recommending FluentValidation for these scenarios?
Of course that I am recommending FluentValidation. It's so obvious that I even no longer mention it :-) I a bit sick of repeating it to be honest.
I can understand that :) Just curious if you were put off FV for any reason. I am still fond of it myself.
Oh no, I am using FluentValidation in every single ASP.NET MVC project I create. It's in my custom project template :-D
[Requiredif] asks for an object TargetValue? I dont get that? What am I suppose to type in there?
0

If the problem were on the client side, jQuery validate will display it. Your problem is on the validation of the server side, as you say, when the ModelState.IsValid is checked. What you will need to do is use CustomValidations for achieve what you need.

You may want to check this out

Comments

0

Instead of marking those 2 properties as required (since they aren't in some cases), I would implement the IValidatableObject interface where you can implement this type of business logic trivially by comparing the values of several properties.

ScottGu has a blog post on exactly how to do this:

http://weblogs.asp.net/scottgu/archive/2010/12/10/class-level-model-validation-with-ef-code-first-and-asp-net-mvc-3.aspx

1 Comment

IValidatableObject does not support client-side validation, which can lead to some odd results (such as the page not validating client-side, fixing the errors reported, then getting errors server-side that were not reported the first time.). This can be annoying to your users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.