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

  • Home
  • SEARCH
  • 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 4255634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:14:43+00:00 2026-05-21T05:14:43+00:00

I am just having a play with HtmlHelpers in MVC, very useful stuff and

  • 0

I am just having a play with HtmlHelpers in MVC, very useful stuff and now i am trying to create one for dropdowns
based on passing in a model ( any ) what the property is for the value and same for text.

so i have something like this:

public static MvcHtmlString DataFilledDropDown<T>(this HtmlHelper h, string name, T selectedValue)
    {
        var myModel = typeof(T);
        var dropDownBox = new Tag("select").With("name", name).And("id", name);

        foreach (T val in Enum.GetValues(myModel))
        {
            var itemText = Resources.ResourceManager.GetString(val.ToString());
            if (String.IsNullOrEmpty(itemText)) itemText = val.ToString();

            var dft = new Tag("option")
                .With("value", "0")
                .AndIf(val.Equals(selectedValue), "selected", "selected")
                .WithText("-- CHOOSE --");
            dropDownBox.Append(dft);

            var option = new Tag("option")
                .With("value", (val).ToString())
                .AndIf(val.Equals(selectedValue), "selected", "selected")
                .WithText(itemText);
            dropDownBox.Append(option);
        }
        return MvcHtmlString.Create(dropDownBox.ToString());

    }

but this would only cover me for single fields but my model could be like this:

public class TestModel{
     public int Id {get; set; } // this i want as a value in dropdown
     public string Name {get;set;} // this i want to be the text value in dropdown
     public bool isActive { get; set; } // this i dont need in dropdown but have data in model
 }

so with above i want to then create something like this:

Html.DataFilledDropDown<myModel>("TestDropdown","Id","Name","0")

where the input is name of dropdown, name of value property, name of text property and default selected value

I have completed this with a little help from the answer below, this is the code for anyone interested:

public static MvcHtmlString DataFilledDropDown<T>(
        this HtmlHelper h, string name, IEnumerable<T> items, 
        Func<T,string> valueField, Func<T,string> textField, string selectedValue)
    {
        var dropDownBox = new Tag("select").With("name", name).And("id", name);
        var defaultValue = "0";
        var dft = new Tag("option")
                .With("value", "0")
                .AndIf(defaultValue.Equals(selectedValue), "selected", "selected")
                .WithText("-- CHOOSE --");
        dropDownBox.Append(dft);

        foreach (var item in items)
        {
            var myValue = valueField(item);
            var myName = textField(item);

            var option = new Tag("option")
                .With("value", myValue)
                .AndIf(myValue.Equals(selectedValue), "selected", "selected")
                .WithText(myName);
            dropDownBox.Append(option);

        }

        return MvcHtmlString.Create(dropDownBox.ToString());
    }

and to run the code

<%: Html.DataFilledDropDown("SpexOptionType", Model.Clubs, x => x.clubID.ToString(), x => x.clubName, "0")%>

thats it

many thanks

  • 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-21T05:14:44+00:00Added an answer on May 21, 2026 at 5:14 am

    I know this doesn’t answer your question directly, but I would suggest you move away from using strings for your name and ids. They are problematic and hard to maintain over time. Instead use Func.

    Here’s what I use for select lists:

    public static string SelectList<T>(this HtmlHelper html, T root, IEnumerable<T> items, Func<T, string> itemValueContent,
                                Func<T, string> itemTextContent, Func<T, IEnumerable<T>> childrenProperty, Func<T, string> parentProperty, string selectSize)
        {
            StringBuilder parentSb = new StringBuilder();
            StringBuilder childSb = new StringBuilder();
    
            parentSb.AppendFormat("<select class='parent' name='parent' size='{0}'>\r\n", selectSize);
            childSb.AppendFormat("<select class='child' id='child' size='{0}'>\r\n", selectSize);
    
            foreach (T parentItem in items)
            {
                foreach (T item in childrenProperty(parentItem))
                {
                    RenderParentOption(parentSb, item, itemValueContent, itemTextContent);
    
                    foreach (T item1 in childrenProperty(item))
                    {
                        RenderOptionWithClass(childSb, item1, itemValueContent, itemTextContent, parentProperty);
                    }
                }                
            }
    
    
    
            parentSb.AppendLine("</select>");
            childSb.AppendLine("</select>");
    
    
            return parentSb.ToString() + childSb.ToString();
        }
    
    
        private static void RenderOptionWithClass<T>(StringBuilder sb, T item, Func<T, string> itemValueContent, Func<T, string> itemTextContent, Func<T, string> parentProperty)
        {
            sb.AppendFormat("<option class='sub_{2}' value='{0}'>{1}</option>\r\n", itemValueContent(item), itemTextContent(item), parentProperty(item));
        }
    
        private static void RenderParentOption<T>(StringBuilder sb, T item, Func<T, string> itemValueContent, Func<T, string> itemTextContent)
        {
            sb.AppendFormat("<option value='{0}'>{1}</option>\r\n", itemValueContent(item), itemTextContent(item) + " ->");
        }
    

    This is how it’s used:

    <%=Html.SelectList<HierarchyNode<CategoryModel>>(Model.CategoryList.First(), Model.CategoryList,
           x => x.Entity.RowId.ToString(),
           x => x.Entity.Name.ToString(),
           x => x.ChildNodes,
           x => x.Entity.ParentCategoryId.ToString(), "10")%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of a play around with IIS7, just trying to catch
I'm just starting to play with asp.net mvc and I've got a very basic
I'm just having a play with Roslyn but unsure on how to do the
I'm completely new to Flex and am just having a play with a sample
I've just started having a play with Kohana, coming from CodeIgniter and straight php.
I have just started to play with Play framework (2.0) and I'm having some
Having just started with MVC 2 I notice that in their starter template they
I was just having a play around with some code in LINQPad and noticed
Was having a play with settings bundles just before in xcode 3.2.3 (sdk 4.0.1),
I'm trying to play animations in sequence but I'm having issues playing them as

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.