Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8344635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:25:59+00:00 2026-06-09T06:25:59+00:00

I have two separate VIEWS accessing the same MODEL. When I put the validator

  • 0

I have two separate VIEWS accessing the same MODEL. When I put the validator data annotations on the model, it works as advertised and prevents the data from being submitted (for both views) if left blank or not within range. However, I have one view that should be able to allow empty or null values to be saved for a property whereas another view needs to require information to be entered or selected before it lets it through. In other words, I’d like to turn off the validator on the property within the MODEL for one view and leave it on for the other view. Here’s the example code:

MODEL:

[Range(1, 999, ErrorMessage = "A submittal is required")]
public int SubmittalId { get; set; }

VIEW #1:

 <label>@Model.AuditDoc.SubmittalsLabel.ConfigurableLabelDesc</label> @Html.ValidationMessageFor(x => x.AuditDoc.SubmittalId) @Html.DropDownListFor(x => x.AuditDoc.SubmittalId, new SelectList(Model.AuditDoc.ListOfSubmittals, "Id", "Name"))

VIEW #2:

 <label>@Model.AuditDoc.SubmittalsLabel.ConfigurableLabelDesc</label> @Html.DropDownListFor(x => x.AuditDoc.SubmittalId, new SelectList(Model.AuditDoc.ListOfSubmittals, "Id", "Name"))

As you can see, I would like to disable that validator data annotation for View #2 and leave it on for View #1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T06:26:02+00:00Added an answer on June 9, 2026 at 6:26 am

    This isn’t possible via the default set of data annotations.. however, you have the choice of using 2 separate view models or writing your own validationAttribute.

    I wrote this once.. although I loathed using it..

    public class RequiredOnAttribute : ValidationAttribute
    {
        public string[] URLs { get; set; }
    
        public override bool IsValid(object value)
        {
            if (URLs.Contains(System.Web.HttpContext.Current.Request.Url.AbsolutePath))
            {
                if (string.IsNullOrEmpty(value as string))
                {
                    return false;
                }
            }
            return true;
        }
    }
    

    Usage:

    [RequiredOn(URLs = new string[] { "/create", "/edit" })]
    public string MyModelField { get; set; }
    

    You could do the same for Range, RegEx, etc..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two separate sets of tables in the same database that model the
I have two separate applications (both part of the same system) that share a
I have two separate views that both show multiple lists of values. Both views
I have two separate views each containing a ScrollViewer. I want to slave one
I have an iPhone app, it has two separate MKMapView components in different views,
So I have two separate views in a WPF Prism app. How can I
I have two separate controllers that inherit from Admin::UserBaseController , display a searchable, sortable
I have a view table which is a union of two separate tables (say
We have two separate web-apps, say 'retailUI' and 'bulkUI'. These two are basically two
I have two separate caches running in a JVM (one controlled by a third

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.