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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:32:15+00:00 2026-06-11T16:32:15+00:00

I am trying to develop an HtmlHelper extension method: EnumDropDownListFor. No matter what I

  • 0

I am trying to develop an HtmlHelper extension method: EnumDropDownListFor. No matter what I did I was unable to show the selected value. I tried setting Selected=true property of SelectListItem and setting selectedValue of SelectList constructor. While debugging (at return line) I can see Selected=true for the SelectLİstItem which is supposed to be Selected, for both cases. But when I “View Source” none of the options have selected=”selected” attribute.

Where am I going wrong?

Note: Toolkit is my utility class and ToByte is an extension method for Enum

    public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(
        this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string optionLabel = null,
        object htmlAttributes = null) where TModel : class
    {
        var selectedValue = helper.ViewData.Model == null
                                ? default(TProperty)
                                : expression.Compile()(helper.ViewData.Model);

        var enumVals = Toolkit.GetEnumValues(typeof(TProperty));

        //var selectList = from enumVal in enumVals.OfType<Enum>()
        //                 select new SelectListItem
        //                 {
        //                     Text = enumVal.GetName(),
        //                     Value = enumVal.ToByte().ToString(),
        //                     Selected = Equals(enumVal, Toolkit.To<Enum>(selectedValue))
        //                 };


        // helper.ViewData[(expression.Body as MemberExpression).Member.Name] = Toolkit.To<Enum>(selectedValue).ToByte().ToString();

        var selectList = new SelectList(from enumVal in enumVals.OfType<Enum>()
                                        select new
                                        {
                                            TextField = enumVal.GetName(),
                                            ValueField = enumVal.ToByte().ToString()
                                        }, "ValueField", "TextField", Toolkit.To<Enum>(selectedValue).ToByte().ToString());

        return helper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
    }
  • 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-06-11T16:32:17+00:00Added an answer on June 11, 2026 at 4:32 pm

    I solved it (:
    That was beacuse I am calling “helper.DropDownListFor” with same expression which returns an Enum type and I was trying to set values of options by “Byte” casted values. So it seems that Expression’s return value overrides the given selected value, makes a lot of sense.

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

Sidebar

Related Questions

I am trying to develop a BlackBerry application that will show data from an
I am trying to develop a Firefox extension. The following code: var divList =
I was trying to develop a method pipeline using asynchronous method invocation. The logic
I´m trying to develop a Native Extension for Flex in order to use the
I'm trying to develop a firefox extension that inserts additional HTTP header fields into
Im trying to develop an application that should show a map using the Google
I'm trying to develop an extension that would add a form on the Admin
I am trying to develop a plugin/extension for a browser (Firefox or IE or
I am trying develop a basic referrer system to my Django website, system will
Im trying to develop service that starts by bootup receiver if the wifi or

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.