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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:18:00+00:00 2026-05-23T21:18:00+00:00

In my viewModel I have public string Addressline1 { get; set; } public List<SelectListItem>

  • 0

In my viewModel I have

public string Addressline1 { get; set; }
public List<SelectListItem> StateList
    {
        get
        {
            return State.GetAllStates().Select(state => new SelectListItem { Selected = false, Text = state.Value, Value = state.Value }).ToList();
        }
    }

In the view I have

@Html.DropDownListFor(model => model.StateCode, Model.StateList, "select")

when AddressLine1 is entered, then the statelist DropDownList selection is Required.How do I validate and show an error message when no state is selected in the Drop down list other than default “select” value?

  • 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-23T21:18:01+00:00Added an answer on May 23, 2026 at 9:18 pm

    Decorate your StateCode property with the [Required] attribute:

    [Required(ErrorMessage = "Please select a state")]
    public string StateCode { get; set; }
    
    public IEnumerable<SelectListItem> StateList
    {
        get
        {
            return State
                .GetAllStates()
                .Select(state => new SelectListItem 
                { 
                    Text = state.Value, 
                    Value = state.Value 
                })
                .ToList();
        }
    }
    

    and then you could add a corresponding validation error message:

    @Html.DropDownListFor(model => model.StateCode, Model.StateList, "select")
    @Html.ValidationMessageFor(model => model.StateCode)
    

    UPDATE:

    Alright it seems that you want to conditionally validate this StateCode property depending on some other property on your view model. Now that’s an entirely different story and you should have explained this in your original question. Anyway, one possibility is to write a custom validation attribute:

    public class RequiredIfPropertyNotEmptyAttribute : ValidationAttribute
    {
        public string OtherProperty { get; private set; }
        public RequiredIfPropertyNotEmptyAttribute(string otherProperty)
        {
            if (otherProperty == null)
            {
                throw new ArgumentNullException("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(CultureInfo.CurrentCulture, "{0} is an unknown property", new object[]
                {
                    OtherProperty
                }));
            }
            var otherPropertyValue = property.GetValue(validationContext.ObjectInstance, null) as string;
            if (string.IsNullOrEmpty(otherPropertyValue))
            {
                return null;
            }
    
            if (string.IsNullOrEmpty(value as string))
            {
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }
    
            return null;
        }
    }
    

    and now decorate your StateCode property with this attribute, like so:

    public string AddressLine1 { get; set; }
    
    [RequiredIfPropertyNotEmpty("AddressLine1", ErrorMessage = "Please select a state")]
    public string StateCode { get; set; }
    

    Now assuming you have the following form:

    @using (Html.BeginForm())
    {
        <div>
            @Html.LabelFor(x => x.AddressLine1)
            @Html.EditorFor(x => x.AddressLine1)
        </div>
    
        <div>
            @Html.LabelFor(x => x.StateCode)
            @Html.DropDownListFor(x => x.StateCode, Model.States, "-- state --")
            @Html.ValidationMessageFor(x => x.StateCode)
        </div>
    
        <input type="submit" value="OK" />
    }
    

    the StateCode dropdown will be required only if the user entered a value into the AddressLine1 field.

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

Sidebar

Related Questions

I have following ViewModel: public IEnumerable<SelectListItem> SelectAdminsInGroup { get; set; } public IEnumerable<SelectListItem> SelectAdminsNotInGroup
So i have something along the lines of private ObservableCollection<ViewModel> _internal; public ObservableCollection<ViewModel> BoundInternal{get;set};
Suppose you have a viewModel: public class CreatePersonViewModel { [Required] public bool HasDeliveryAddress {get;set;}
I have TabItem class: public class TabItem { public string Header { get; set;
Suppose I have ViewModel like public class AnotherViewModel { public string Name { get;
I can't get a select list to bind to my ViewModel. I have a
I have following ViewModel: public class Bulletin1ViewModel { [Required] public String NumberDelegations { get;
I have the classes: public class PersonDetailsModel { public string FistName { get; set;
I have a ViewModel: public class Page { public int Id { get; set;
I have a class : public class Car { public string Name { get;

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.