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

The Archive Base Latest Questions

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

I am trying to create a dropdown list to display all the value in

  • 0

I am trying to create a dropdown list to display all the value in a custom collection class
such as

public class MyCustomClassCollection
{

public List<MyCustomClass> {get;set;}

}

I want it to show the Description:string of each MyCustomClass

I tried

<%: Html.DropDownList "Description", MyCustomClass %>

Resharper suggests that I cast MyCustomClass to IEnemerable

but the server returns an unable to cast error.

Any Idea how I can create this DropDownList?

__Modification___

public class ViewModel
    {
        public Detail detail { get; set; }
    }

public class Detail   //Inherited from webservce
{
   public CustomClassCollection {get;set;}
   .... Other Properties, a.k.a Custom Classes
}


public class CustomClassCollection
{
   public List<CustomClass> {get;set;}
}

public class CustomClass {
  public int Id {get;set;}
  public string Description{get;set;}
  ... other properties

}


public ActionResult Index(int? id, DateTime? date)
        {
            if (id.Equals(null))
                id = ######### ;
            if (date.Equals(null))
                date = DateTime.Today;
            var vm = new ViewModel
                         {
                             Detail = _repository.Detail((int)id,(DateTime)date)
                         };
            return View(vm);
        }
  • 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-25T16:04:41+00:00Added an answer on May 25, 2026 at 4:04 pm

    The second argument of the DropDownList helper must be an IEnumerable<SelectListItem> or a SelectList which implements this interface for that matter. So in your controller action organize in such a way that you convert your custom collection into an IEnumerable<SelectListItem>. As always you could start by writing a view model:

    public class MyViewModel
    {
        public string SelectedDescription { get; set; }
        public SelectList Descriptions { get; set; }
    }
    

    and then have your controller action query the custom list and populate the view model which will be passed to the view:

    public ActionResult Index()
    {
        var descriptions = yourCustomCollection.MyCustomClass.Select(x => new
        {
            Value = x.Description,
            Text = x.Description
        });
        var model = new MyViewModel
        {
            Descriptions = new SelectList(descriptions, "Value", "Text")
        };
        return View(model);
    }
    

    and finally in your strongly typed view:

    <%= Html.DropDownListFor(x => x.SelectedDescription, Model.Decriptions) %>
    

    UPDATE:

    After posting your updated models (which by the way are still incomplete and impossible to compile as you haven’t provided any property names), here’s an example:

    public class ViewModel
    {
        public int SelectedId { get; set; }
        public Detail Detail { get; set; }
    }
    
    public class Detail
    {
       public CustomClassCollection MyCollection { get; set; }
    }
    
    
    public class CustomClassCollection
    {
       public List<CustomClass> CustomClass { get; set; }
    }
    
    public class CustomClass 
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }
    
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var vm = new ViewModel
            {
                Detail = new Detail
                {
                    MyCollection = new CustomClassCollection
                    {
                        CustomClass = new List<CustomClass>
                        {
                            new CustomClass
                            {
                                Id = 1,
                                Description = "description 1",
                            },
                            new CustomClass
                            {
                                Id = 2,
                                Description = "description 2",
                            },
                            new CustomClass
                            {
                                Id = 3,
                                Description = "description 3",
                            },
                        }
                    }
                }
            };
            return View(vm);
        }
    }
    

    and in the view:

    <%= Html.DropDownListFor(
        x => x.SelectedId, 
        new SelectList(Model.Detail.MyCollection.CustomClass, "Id", "Description")
    ) %>
    

    What you have to understand in order to define a dropdown list in ASP.NET MVC ius that you need 2 things:

    1. A scalar property to bind the selected value to (SelectedId in my example)
    2. A collection to bind the list to (Model.Detail.MyCollection.CustomClass in the example)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create 2 dropdown menus. One for displaying a list of
I am trying to create a drop down list box with the selected value
Relatively new to MVC and trying to get a cascading dropdown list working for
I'm trying to create a dropdown list with 'select' html tag , however ,
I am trying to create a dropdown list using struts. I want to load
I'm trying to create a dropdown that upon changing the selection from the list
I'm trying to create a custom drop down and using the code below it
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
I am trying to create dropdown menu for Wordpress theme and having some silly
I am trying to create a dropdown menu. It working well. I want the

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.