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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:24:04+00:00 2026-05-23T18:24:04+00:00

Controller var productList = Enumerable.Range(1, 80).Select( x => new SelectListItem { Value = x.ToString(),

  • 0

Controller

var productList = Enumerable.Range(1, 80).Select(
    x => new SelectListItem { Value = x.ToString(), Text = x.ToString() }
);
ViewData["products"] = new SelectList(productList.ToList(), "Value", "Text");

View

<%: Html.DropDownList("products", ViewData["products"] as SelectList, "--select--")%>
<%: Html.ValidationMessage("products", "Please Select the Product from the List")%> 
//This doesnt works on (ModelState.IsValid) I know the dropdown list data is coming
//from the view data not model , thats why model doesnt validate the particular dropdown 
//list while it validates other fields which are linked to model,
//Just want to know, how can i validate the above dropdownlist 
  • 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-23T18:24:05+00:00Added an answer on May 23, 2026 at 6:24 pm

    You are binding both the name of the ddl and the values to products. That’s wrong. Of course that’s the least problem with your code. The far bigger and more serious problem is that you are using ViewData instead of using strongly typed views and view models.

    So there are two possibilities:

    1. The crappy one: have a property on your view model to which you will bind your dropdown value.

      [Required(ErrorMessage = "Please Select the Product from the List")]
      public string SelectedProduct { get; set; }
      

      and then use this property name as first argument to the weakly typed DropDownList helper and the ViewData as second agrument:

      <%= Html.DropDownList( 
          "SelectedProduct", 
          ViewData["products"] as SelectList, 
          "--select--"
      ) %>
      <%= Html.ValidationMessage("SelectedProduct") %>
      
    2. The correct way: which of course is using real view models (I am sick of repeating it, just google, you will get like gazillions of answers, just by me, and just on this site on this topic). It will look like this:

      <%: Html.DropDownListFor( 
          x => x.SelectedProduct, 
          Model.Products, 
          "--select--"
      ) %>
      <%= Html.ValidationMessageFor(x => x.SelectedProduct) %>
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple controller test using MvcContrib's test helpers: var controller = new
I have a list which I created in the controller: var PayList = new[]
I have a self calling function. var controller = new (function () { this.func
tried using the following but it's not the solution controller: var list = new
In controller: var jsonData = new { total = totalPages, page = pageIndex, records
I have the following code in my controller: public ActionResult Details(int id) { var
I have the CakePHP stack in /var/www/site from one view under controller A I
I have the following in my controller: var $cacheAction = array( 'view/' => 432000,
I need to do something like this is this possible? <% var Controller =
How would I do something like this: <?php class Controller { var $ActionName; var

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.