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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:04:40+00:00 2026-05-13T15:04:40+00:00

I’m trying to use the built in ASP.NET MVC 2 client side validation on

  • 0

I’m trying to use the built in ASP.NET MVC 2 client side validation on a Select List like the following:

 private SelectList _CategoryList;
        [Required(ErrorMessage = "Category Required")]
        [System.ComponentModel.DataAnnotations.Range(1, double.MaxValue, ErrorMessage = "Please Select A Category")]
        [DisplayName("Category")]
        public SelectList CategoryList
        {
            get
            {
                return new SelectList(Categories, "CatID", "CatFullName"); ;
            }
            set
            {
                _CategoryList = value;
            }
        }

However it’s not working…if the default value which is 0 is selected the validation message does not appear and the page progresses as though it’s validated. Thoughts?

  • 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-13T15:04:40+00:00Added an answer on May 13, 2026 at 3:04 pm

    Ok so I found the answer in an answer to a slightly different question. So I’m posting my complete code here, which extends on Scott Guthries ASP.NET MVC 2 Validation post: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

    My ViewModel:

    public class Person
    {
        [Required(ErrorMessage="First Name Required")]
        [StringLength(50,ErrorMessage="Must be under 50 characters")]
        public string FirstName { get; set; }
    
        [Required(ErrorMessage="Last Name Required")]
        [StringLength(50, ErrorMessage = "Must be under 50 characters")]
        public string LastName { get; set; }
    
        [Required(ErrorMessage="Age Required")]
        [Range(1,120,ErrorMessage="Age Must be between 0 and 120")]
        public int Age { get; set; }
    
        [Required(ErrorMessage="Email Required")]
        public string Email { get; set; }
    
    
        public IEnumerable<SelectListItem> FavoriteColor { get; set; }
    
    
        [Range(0, 6, ErrorMessage = "Out of range")]
        public int SelectedFavColor { get; set; }
    }
    

    My Color class:

    public class Colors
    {
        public int ColorID { get; set; }
        public string ColorName { get; set; }
    }
    

    My list helper extensions stolen from Rob Connery, who stole it from someone else:

    public static class ListExtensions
    {
        public static IEnumerable<T> ForEach<T>(this IEnumerable<T> collection, Action<T> action)
        {
            foreach (var item in collection) action(item);
            return collection;
        }
    
        public static SelectList ToSelectList<T>(this IEnumerable<T> collection)
        {
            return new SelectList(collection, "Key", "Value");
        }
    
        public static SelectList ToSelectList<T>(this IEnumerable<T> collection, string selectedValue)
        {
            return new SelectList(collection, "Key", "Value", selectedValue);
        }
    
        public static SelectList ToSelectList<T>(this IEnumerable<T> collection,
                             string dataValueField, string dataTextField)
        {
            return new SelectList(collection, dataValueField, dataTextField);
        }
    
        public static SelectList ToSelectList<T>(this IEnumerable<T> collection,
                             string dataValueField, string dataTextField, string selectedValue)
        {
            return new SelectList(collection, dataValueField, dataTextField, selectedValue);
        }
    }
    

    My Controller Code (yes it could be refactored to be more DRY):

    public ActionResult Create()
        {
            Person newFriend = new Person();
            IList<Colors> colorslist = new List<Colors>();
            colorslist.Add(new Colors { ColorID = -1, ColorName = "Please Select Color" });
            colorslist.Add(new Colors { ColorID = 1, ColorName = "Red" });
            colorslist.Add(new Colors { ColorID = 2, ColorName = "Green" });
            colorslist.Add(new Colors { ColorID = 3, ColorName = "Blue" });
    
            newFriend.FavoriteColor = colorslist.ToSelectList("ColorID","ColorName","-1");
            return View(newFriend);
        }
    
        [HttpPost]
        public ActionResult Create(Person friendToCreate, FormCollection collection)
        {
            friendToCreate.SelectedFavColor = Convert.ToInt32(collection["SelectedFavColor"]);
            if (ModelState.IsValid)
            {
                return Redirect("/");
            }
            IList<Colors> colorslist = new List<Colors>();
            colorslist.Add(new Colors { ColorID = -1, ColorName = "Please Select Color" });
            colorslist.Add(new Colors { ColorID = 1, ColorName = "Red" });
            colorslist.Add(new Colors { ColorID = 2, ColorName = "Green" });
            colorslist.Add(new Colors { ColorID = 3, ColorName = "Blue" });
            friendToCreate.FavoriteColor = colorslist.ToSelectList("ColorID", "ColorName");
            return View(friendToCreate);
        }
    

    My page markup:

    <% using (Html.BeginForm()) {%>
    
        <fieldset>
            <legend>Fields</legend>
    
            <div class="editor-label">
                <%= Html.LabelFor(model => model.FirstName) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.FirstName) %>
                <%= Html.ValidationMessageFor(model => model.FirstName) %>
            </div>
    
            <div class="editor-label">
                <%= Html.LabelFor(model => model.LastName) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.LastName) %>
                <%= Html.ValidationMessageFor(model => model.LastName) %>
            </div>
    
            <div class="editor-label">
                <%= Html.LabelFor(model => model.Age) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.Age) %>
                <%= Html.ValidationMessageFor(model => model.Age) %>
            </div>
    
            <div class="editor-label">
                <%= Html.LabelFor(model => model.Email) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.Email) %>
                <%= Html.ValidationMessageFor(model => model.Email) %>
            </div>
    
            <div class="editor-label">
                <%= Html.LabelFor(model => model.FavoriteColor) %>
            </div>
            <div class="editor-field">
                <%= Html.DropDownList("SelectedFavColor", Model.FavoriteColor, -1)%>
                <%= Html.ValidationMessageFor(model => model.SelectedFavColor) %>
            </div>
    
            <p>
                <input type="submit" value="Submit" />
            </p>
        </fieldset>
    
    <% } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.