Solved

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

Posted on 2010-11-15
2
2,104 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!

820 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