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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:25:53+00:00 2026-05-23T15:25:53+00:00

Im trying to create an dropdownlist from an IList with following syntax: @Html.DropDowntListFor(Model.VisitingAddresses, vistingAddress

  • 0

Im trying to create an dropdownlist from an IList with following syntax:

@Html.DropDowntListFor(Model.VisitingAddresses, vistingAddress => vistingAddress.Id, vistingAddress => vistingAddress.Name)

This works with the following code:

public static IHtmlString DropDowntListFor
        <TModel>(this HtmlHelper htmlHelper, IList<TModel> list, Expression<Func<TModel, string>> value, Expression<Func<TModel, string>> text)
    {
        var dropdownName = value.Parameters.First().Name;

        var selectedListItem = new List<SelectListItem>();

        var values = list.AsQueryable().Select(value).ToList();
        var texts = list.AsQueryable().Select(text).ToList();

        int i;
        for (i = 0; i < values.Count; i++)
        {
            selectedListItem.Add(new SelectListItem
                                     {
                                        Value = values[i],
                                        Text = texts[i]
                                     });
        }

        return htmlHelper.DropDownList(dropdownName, selectedListItem);
    }

But as you can see the code above (in the htmlhelper) is really really ugly, is there someone that knows an more beautiful way (in code) for in the html helper?

Thanks in advance.

  • 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-23T15:25:53+00:00Added an answer on May 23, 2026 at 3:25 pm

    Are you after something like this?

    public static IHtmlString DropDowntListFor<TModel>
        (this HtmlHelper htmlHelper, IList<TModel> list, 
         Expression<Func<TModel, string>> valueSelector, 
         Expression<Func<TModel, string>> textSelector)
    {
        var dropdownName = valueSelector.Parameters.First().Name;
    
        Func<TModel, string> compiledValueSelector = valueSelector.Compile();
        Func<TModel, string> compiledTextSelector = textSelector.Compile();
    
        var selectedListItem = list.Select(x => new SelectListItem {
                                               Value = compiledValueSelector(x),
                                               Text = compiledTextSelector(x) })
                                   .ToList();
    
        return htmlHelper.DropDownList(dropdownName, selectedListItem);
    }
    

    Note that if you don’t need the text selector as an expression tree, you can simplify it slightly further:

    public static IHtmlString DropDowntListFor<TModel>
        (this HtmlHelper htmlHelper, IList<TModel> list, 
         Expression<Func<TModel, string>> valueSelectorExpression, 
         Func<TModel, string> textSelector)
    {
        var dropdownName = valueSelector.Parameters.First().Name;
    
        var valueSelector = valueSelectorExpression.Compile();
    
        var selectedListItem = list.Select(x => new SelectListItem {
                                               Value = valueSelector(x),
                                               Text = textSelector(x) })
                                   .ToList();
    
        return htmlHelper.DropDownList(dropdownName, selectedListItem);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I'm new to asp.net. I'm trying to create two linked dropdownlist i.e if first
I am trying to create a subclass of asp:DropDownList so I can use asp:DdlNoEventValidation
In rails 3, how to create a Dropdown from hash I have following code
I am trying to create a dropdown list that contains values from my data
I was trying to create a custom combobox that inherited from ComboBox, but I've
I'm trying to create two dropdownlist, the first one with a list of users,
I am trying to create a site where you select an option from a
I am trying to create an Item that has an ItemType coming from another
I'm running into an issue trying to use @Html.DropDownListFor(). I have a model with

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.