Solved

ASP.NET MVC - Disable required data annotation in view model

Posted on 2010-11-15
2
2,073 Views
Last Modified: 2012-05-10
I'm working in an application written in ASP.NET MVC.  I have a requirement on a certain page to make one of the required fields no longer be a required field.  This particular field is made required by adding the data annotation in the view model like this...

[Required(ErrorMessage = "Customer ID is required.")]
[StringLength(20, ErrorMessage = "Customer ID cannot be longer than {1} characters.")]
public string CustomerCode { get; set; }

However, the page I'm working on does not directly use the view model where this is defined.  The code above exists in ViewModel A and ViewModel A is used in multiple different locations throughout the application.  The page I am working on utilizes ViewModel B which inherits and extends ViewModel A.  I don't want to make a change to ViewModel A directly to remove the required field data annotation because there is no telling what else will be impacted by that throughout the application.  But I do need it to be deactivated for ViewModel B.

So my question: is there a way inside of ViewModel B to disable the data annotation created in ViewModel A that it inherits?

Thanks guys,
0
Comment
Question by:JosephEricDavis
2 Comments
 
LVL 16

Accepted Solution

by:
ToddBeaulieu earned 500 total points
ID: 34139092
Just learning this stuff myself. Figured I'd throw a couple of ideas out there for you.

If this were me, I'd make the base VM abstract (immediately flush out references to it) and then make a new VM-A for those previously using the base.

I'd think this would then allow you to attribute the property on VM-A and VM-B independently. If that works, this should be quick, no-risk fix.

I also came across someone making a custom attribute, which I think is interesting. You could make a new required "if" attribute.

I prefer the first approach, though.

http://efreedom.com/Question/1-3713281/Required-Attribute-Dependent-Another-Field
0
 
LVL 7

Author Closing Comment

by:JosephEricDavis
ID: 34146572
Good thought.  But that would have required some pretty extensive changes throughout the application in all the different places where this base class viewmodel is being used.

I just ended up writing some logic that did some checks and forced the the model to be valid if it was invalid by reason of the required field data annotation.

Thanks for participating though.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now