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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:05:27+00:00 2026-05-27T15:05:27+00:00

I created an HtmlHelper method called DisplayListBoxFor that will need display a textual representation

  • 0

I created an HtmlHelper method called DisplayListBoxFor that will need display a textual representation of the selected items in a MultiSelectList as an unordered list (UL/LI) in the browser. I have specific reasons to keep the signature between the standard ListBoxFor and my custom DisplayListBoxFor exactly the same, which means that my method needs to accept a lambda expression. I have the following code in my helpers class that is modeled off of the Html.ListBoxFor method:

public static MvcHtmlString DisplayListBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
{
  TagBuilder tag = new TagBuilder("ul");
  foreach (var value in ((MultiSelectList)selectList).SelectedValues)
  {
    TagBuilder itemTag = new TagBuilder("li");
    itemTag.SetInnerText(value.ToString());
    tag.InnerHtml += itemTag.ToString();
  }
  return new MvcHtmlString(tag.ToString());
}

I can then call my custom method like so:

@Html.DisplayListBoxFor(x => Model.MySelectedValues, Model.MyAvailableValues)

The types I used are as follows:

Model.MySelectedValues = List<Object> 
Model.MyAvailableValues = MultiSelectList

This works just fine, with the exception that the output only lists the ID value and not the name of the selected value because that is all that is stored in the MultiSelectList.SelectedValues property upon creation. I am trying to find a way in my foreach loop to match up the selected value to the value name, but since everything within the method is an anyonymous type, I can’t cast it to any non-concrete values or even use an Indexer to find the id and name properties. How would I be able to get these values from the anonymous types passed to this method?

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

    I figured out a solution to this, but I posted in a separate post where I “dumbed down” the question. See MVC Method to output unordered list from the ListBoxFor method

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

Sidebar

Related Questions

I have created an HtmlHelper Extension method which returns an encoded string, I have
I want to create an extension method for HtmlHelper that allows me to create
I created an extension method for the HtmlHelper which works very well. Now I
I have the following HtmlHelper method that I want to create a button that
I'm trying to create an Extension Method for MVC's htmlHelper. The purpose is to
I'm not seeing a way to create, via the HtmlHelper , a SelectListItem that
I've created an Html extension method in Helper class, but I can not get
I'm creating a tabcontainer that shows information from partials. The code that i've created
I want to create a simple extension of HtmlHelper.ActionLink that adds a value to
I've created a extension method of Jquery datepicker, below are the source code. public

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.