[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-11-15
2
Medium Priority
?
2,187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
ToddBeaulieu earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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