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 6772317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:32:08+00:00 2026-05-26T15:32:08+00:00

I’ve got an MVC app which features a DropDownList and TextBox. I’m using MVC

  • 0

I’ve got an MVC app which features a DropDownList and TextBox. I’m using MVC validation and I’m happy with it.

At the moment The DropDownList is [Required], but I want to make it so the TextBox can function as an ‘other: please specify’ style input for the DropDownList.

How do I make it so that the [Required] attribute on the DropDownList is conditional on the TextBox being empty?

This question is similar, but it’s over a year old. Anything in the current version of MVC that makes this easy to do?

  • 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-05-26T15:32:09+00:00Added an answer on May 26, 2026 at 3:32 pm

    It would be pretty easy to write a custom validation attribute:

    public class RequiredIfPropertyIsEmptyAttribute : RequiredAttribute
    {
        private readonly string _otherProperty;
        public RequiredIfPropertyIsEmptyAttribute(string otherProperty)
        {
            _otherProperty = otherProperty;
        }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var property = validationContext.ObjectType.GetProperty(_otherProperty);
            if (property == null)
            {
                return new ValidationResult(string.Format("Unknown property {0}", _otherProperty));
            }
    
            var otherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);
            if (otherPropertyValue == null)
            {
                return base.IsValid(value, validationContext);
            }
            return null;
        }
    }
    

    then you could have a view model:

    public class MyViewModel
    {
        public string Foo { get; set; }
    
        [RequiredIfPropertyIsEmpty("Foo")]
        public string SelectedItemId { get; set; }
    
        public IEnumerable<SelectListItem> Items {
            get
            {
                return new[] 
                {
                    new SelectListItem { Value = "1", Text = "item 1" },
                    new SelectListItem { Value = "2", Text = "item 2" },
                    new SelectListItem { Value = "3", Text = "item 3" },
                };
            }
        }
    }
    

    A controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    And of course a view:

    @model MyViewModel
    
    @using (Html.BeginForm())
    {
        <div>
            @Html.LabelFor(x => x.Foo)
            @Html.EditorFor(x => x.Foo)
        </div>
        <div>
            @Html.LabelFor(x => x.SelectedItemId)
            @Html.DropDownListFor(x => x.SelectedItemId, Model.Items, "-- select an item --")
            @Html.ValidationMessageFor(x => x.SelectedItemId)
        </div>
    
        <input type="submit" value="OK" />
    }
    

    or you could do as I do: download and use the FluentValidation.NET library, forget about data annotations and write the following validation logic which seems pretty self-explanatory:

    public class MyViewModelValidator: AbstractValidator<MyViewModel> 
    {
        public MyViewModelValidator() 
        {
            RuleFor(x => x.SelectedItemId)
                .NotEmpty()
                .When(x => !string.IsNullOrEmpty(x.Foo));
        }
    }
    

    Just go ahead and Install-Package FluentValidation.MVC3 and make your life easier.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I want to construct a data frame in an Rcpp function, but when I
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.