?
Solved

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

Posted on 2010-11-15
2
Medium Priority
?
2,153 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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…
Suggested Courses

752 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