Solved

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…

739 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