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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:20:01+00:00 2026-05-27T14:20:01+00:00

Is there a way to trigger client-side validation for the select list created by

  • 0

Is there a way to trigger client-side validation for the select list created by DropDownListFor? The select list that gets created by the helper doesn’t seem to get the “data-val” or “data-val-required” attributes that text inputs get for client-side validation.

The validation does occur on the server-side when I check ModelState.IsValid, and the validation message is then displayed on the subsequent page load.

I set a default option of “Please Select…” on the list because I want the user to have to choose an option (as opposed to having it select the first item in the list).

My View Model:

public partial class ProvinceVM
{
    [Required]
    [Range(1, int.MaxValue)]
    public int CountryId { get; set; }
    [Required]
    [StringLength(16)]
    public string Abbreviation { get; set; }
    [Required]
    [StringLength(64)]
    public string Name { get; set; }
}

The code in the View:

<div class="editor-label">
    @Html.LabelFor(model => model.CountryId, "Country")
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.CountryId, (IEnumerable<SelectListItem>)ViewBag.CountryId, "Please Select...") 
    @Html.ValidationMessageFor(model => model.CountryId)
</div>

And my controller code:

[HttpPost]
public ActionResult Create(ProvinceEditVM provinceVM)
{
    if (ModelState.IsValid)
    {
        var province = new Province() { CountryId = provinceVM.CountryId.Value, Abbreviation = provinceVM.Abbreviation, Name = provinceVM.Name };
        _repo.Add<Province>(province);
        _repo.SaveChanges();
        return RedirectToAction("index");  
    }
    ViewBag.CountryId = new SelectList(_repo.GetQuery<Country>().OrderBy(x => x.Name), "CountryId", "Name", provinceVM.CountryId);
    return View(provinceVM);
}

Things I’ve already tried:

  • Making the View Model’s CountryId property a nullable int (int?)
  • Removing the Range attribute for the CountryId property
  • Renaming the ViewBag.CountryId to something else (in case there were some conflict with the View Model’s property name)

I can easily make the value required using jQuery, but I want to make sure I’m not overlooking something that is already built in; especially since further down the road I’d like to add localized culture error messages.

  • 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-27T14:20:01+00:00Added an answer on May 27, 2026 at 2:20 pm

    Was actually able to get it working by doing a combination of all three things I mentioned at the end of my post. Made the property on the View Model nullable, required, and had to rename the ViewBage.CountryId to something else (I renamed it to CountryList).

    I figured it out by adding a SelectList property to my ProvinceVM to get it out of the ViewBag, then all of a sudden the data-val* attributes were showing up on the list.

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

Sidebar

Related Questions

Is there a way to detect via JavaScript (client side) any AJAX requests that
does anyone know if there is a way to trigger the client side form
Is there a way that I could trigger an onresize() event for my window
Is there a way to trigger the validation on the form on the first
I have a form that is using jQuery Validate plugin for client side validation,
Is there a way to trigger a facebook dialog with list of friends in
Is there a way to trigger a beep/alarm/sound when my breakpoint is hit? I'm
Is there a way to trigger a GridView rebind from an html markup page
In SQL Server 2005, is there a way for a trigger to find out
Is there any way to automatically create a trigger on creation of new table

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.