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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:22:01+00:00 2026-06-18T10:22:01+00:00

I am creating a custom helper method for a Drop Down List with the

  • 0

I am creating a custom helper method for a Drop Down List with the following signature:

public static MvcHtmlString MyCustomDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes)

Within the method, I can do the following to get the selected value:

var Value = ((SelectList)selectList).SelectedValue);

In my helper method, I need to find out what the selected text is as well, and not just the value. How will I get that?

  • 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-18T10:22:02+00:00Added an answer on June 18, 2026 at 10:22 am

    If you already have selected value you can just iterate through the IEnumerable trying to find the selected SelectListItem:

            SelectListItem[] items = selectList.ToArray();
            SelectListItem selectedItem = items.FirstOrDefault(i => i.Value == valueAsString) 
                ?? items[0];
            string selectedText = selectedItem.Text;
    

    From my opinion, casting to SelectList is not the best option to find selected value, this casting could throw InvalidCastException when real type of selectList variable is other than SelectList.

    In most cases your model contains a value that should be selected in the drop down list. You can get this value using ModelMetadata classes as follows:

            //Get value from model or from ModelState
            object modelValue = ModelMetadata
                 .FromLambdaExpression(expression, html.ViewData).Model;
    
            SelectListItem[] items = selectList.ToArray();
    
            string selectedValue = modelValue == null ? null : modelValue.ToString();
    
            SelectListItem selectedItem = items.FirstOrDefault(i => i.Value == selectedValue) 
                ?? items.FirstOrDefault();
    
            string selectedText = selectedItem == null ? null : selectedItem.Text;
    
            //Rest code goes here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a custom helper to create image links as follows: public static MvcHtmlString
I'm looking into creating a custom helper to do the following: <a href=controller/> <strong>Item</strong>
I'm often creating custom assertion methods for my JUnit tests. eg: public void assertArrays(String[]
There are two ways of creating a custom HTML helper in ASP.NET MVC: You
I am creating a custom helper in ./application/helpers however i am getting this error
I am creating my own helper in MVC. But the custom attributes are not
I'm currently creating some custom helper classes, similar to ASP.NET MVC's standard HtmlHelper .
Creating custom IHttpModules, I have realized that the requests for static files (e.g.: .css
I am creating custom ViewGroup in which I am using onLayout method in which
I'm creating a custom html helper that will allow me to group multiple checkboxes

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.