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

  • Home
  • SEARCH
  • 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 8857863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:41:39+00:00 2026-06-14T14:41:39+00:00

I am trying to use client-side validation on a select element, but it’s not

  • 0

I am trying to use client-side validation on a select element, but it’s not working when the form initially loads.

This is the property in my Model (this is actually inside a class inside my model):

[Required(ErrorMessage="Please select an application type.")]
public override string Application
{
    get
    {
        return base.Application;
    }
    set
    {
        base.Application = value;
    }
}

This is the part of the view

<div class="field_title">
    Application Type
</div>
<div class="field_validation">
    @Html.ValidationMessageFor(model => model.CreatePromocode.Application)
</div>
<div class="form_field">
    @Html.DropDownListFor(model => model.CreatePromocode.Application,
        SharedLibrary.Entities.DropDownLists.PromocodeApplicationOptions,
        "Select an application type...")
</div>

This is the dropdownlist items code, I’m doing it this way so I can use it on other applications and just change it in one place rather than in several

public class DropDownLists
{        
    public static List<SelectListItem> PromocodeApplicationOptions
    {
        get
        {
            return new List<SelectListItem>()
            {
                new SelectListItem() { 
                    Text="Earlybird Registration", Value="Earlybird"},
                new SelectListItem() { 
                    Text="Standard Registration", Value="Standard"},
                new SelectListItem() { 
                    Text="Monday Registration", Value="Monday"},
                new SelectListItem() { 
                    Text="Tuesday Registration", Value="Tuesday"},
                new SelectListItem() { 
                    Text="Wednesday Registration", Value="Wednesday"},
                new SelectListItem() { 
                    Text="Multiple Tickets", Value="TicketMany"},
                new SelectListItem() { 
                    Text="Single Ticket",Value="TicketSingle"}
            };
        }
    }
}

The problem I am having is that when a user first arrives on the page, the drop down list does not have any client-side validation attributes. It looks like this

<select id="CreatePromocode_Application" name="CreatePromocode.Application">

Once the form is posted however, and the ModelState is invalid because the application type hasn’t been selected, the client validation then appears on the drop down list and works fine.

<select data-val="true" 
    data-val-required="Please select an application type." 
    id="CreatePromocode_Application"
    name="CreatePromocode.Application" 
    class="valid">

How can I get client-side validation working from the beginning? I would rather not have to manually add required attributes and error messages because it kind of defeats the purpose of using data annotations.

  • 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-14T14:41:40+00:00Added an answer on June 14, 2026 at 2:41 pm

    Make sure you are initializing the CreatePromoCode property of your view model as in the default constructor of this sample:

    public class YourViewModel
    {
        public YourViewModel()
        {
            CreatePromoCode = new CreatePromoCode();
        }
    
        public BaseCreatePromoCode CreatePromoCode { get; set; }
    
    }
    
    public class BaseCreatePromoCode
    {
        public virtual string Application { get; set; }
    
    }
    
    public class CreatePromoCode : BaseCreatePromoCode
    {
        [Required]
        public override string Application { get; set; }
    }
    

    The HtmlHelper will use the runtime type of the CreatePromoCode property of your view model in order to get the metadata for its properties and set the data attributes for the client side validation. (So the validation will work even when the Required attribute is actually set only on the derived class.)

    When there are errors after the initial posting, the MVC binder has created instances of the view model and the CreatePromoCode objects. If you render again the view for errors to be displayed, as the object has been initialized the data attributes will be set and the client validation starts working.

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

Sidebar

Related Questions

I'm trying to use MVC2 client-side validation in a partial view that is rendered
I am trying to use jquery to do a client side validation to do
I am trying to use the jQuery validation plugin to add client side validation.
I'm trying to use the example from this site to do client site validation.
I'm trying out ASP.NET MVC 2 Preview and when I use client side validation
I am trying to use ZeroMQ's pub sub messaging, but the client side requires
I am trying to use this simple code in WCF: Client Side: ServiceContractClient proxy
I'm trying to use the built in ASP.NET MVC 2 client side validation on
I have Results page that contains an signup form. I'm trying to use client-side
I am trying to use the Client-side Validation with jQuery in ASP.NET MVC (2).

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.