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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:31:55+00:00 2026-05-14T04:31:55+00:00

There was an Html.RadioButtonList extension method in ASP.NET MVC Futures. Has anyone found a

  • 0

There was an Html.RadioButtonList extension method in ASP.NET MVC Futures. Has anyone found a code for a strongly typed version RadioButtonListFor<T>. It would look like this in a view:

<%= Html.RadioButtonListFor(model=>model.Item,Model.ItemList) %>
  • 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-14T04:31:55+00:00Added an answer on May 14, 2026 at 4:31 am

    Here is the usage in the aspx page

        <%= Html.RadioButtonListFor(m => m.GenderRadioButtonList)%>
    

    Here is the view model

    public class HomePageViewModel
    {
        public enum GenderType
        {
            Male,
            Female
        }
        public RadioButtonListViewModel<GenderType> GenderRadioButtonList { get; set; }
    
        public HomePageViewModel()
        {
            GenderRadioButtonList = new RadioButtonListViewModel<GenderType>
            {
                Id = "Gender",
                SelectedValue = GenderType.Male,
                ListItems = new List<RadioButtonListItem<GenderType>>
                {
                    new RadioButtonListItem<GenderType>{Text = "Male", Value = GenderType.Male},
                    new RadioButtonListItem<GenderType>{Text = "Female", Value = GenderType.Female}
                }
            };
        }
    }
    

    Here’s the view model used for radio button lists

    public class RadioButtonListViewModel<T>
    {
        public string Id { get; set; }
        private T selectedValue;
        public T SelectedValue
        {
            get { return selectedValue; }
            set
            {
                selectedValue = value;
                UpdatedSelectedItems();
            }
        }
    
        private void UpdatedSelectedItems()
        {
            if (ListItems == null)
                return;
    
            ListItems.ForEach(li => li.Selected = Equals(li.Value, SelectedValue));
        }
    
        private List<RadioButtonListItem<T>> listItems;
        public List<RadioButtonListItem<T>> ListItems
        {
            get { return listItems; }
            set
            {
                listItems = value;
                UpdatedSelectedItems();
            }
        }
    }
    
    public class RadioButtonListItem<T>
    {
        public bool Selected { get; set; }
    
        public string Text { get; set; }
    
        public T Value { get; set; }
    
        public override string ToString()
        {
            return Value.ToString();
        }
    }
    

    Here’s the extension methods for RadioButtonListFor

    public static class HtmlHelperExtensions
    {
        public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression) where TModel : class
        {
            return htmlHelper.RadioButtonListFor(expression, null);
        }
    
        public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression, object htmlAttributes) where TModel : class
        {
            return htmlHelper.RadioButtonListFor(expression, new RouteValueDictionary(htmlAttributes));
        }
    
        public static string RadioButtonListFor<TModel, TRadioButtonListValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel<TRadioButtonListValue>>> expression, IDictionary<string, object> htmlAttributes) where TModel : class
        {
            var inputName = GetInputName(expression);
    
            RadioButtonListViewModel<TRadioButtonListValue> radioButtonList = GetValue(htmlHelper, expression);
    
            if (radioButtonList == null)
                return String.Empty;
    
            if (radioButtonList.ListItems == null)
                return String.Empty;
    
            var divTag = new TagBuilder("div");
            divTag.MergeAttribute("id", inputName);
            divTag.MergeAttribute("class", "radio");
            foreach (var item in radioButtonList.ListItems)
            {
                var radioButtonTag = RadioButton(htmlHelper, inputName, new SelectListItem{Text=item.Text, Selected = item.Selected, Value = item.Value.ToString()}, htmlAttributes);
    
                divTag.InnerHtml += radioButtonTag;
            }
    
            return divTag + htmlHelper.ValidationMessage(inputName, "*");
        }
    
        public static string GetInputName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
        {
            if (expression.Body.NodeType == ExpressionType.Call)
            {
                var methodCallExpression = (MethodCallExpression)expression.Body;
                string name = GetInputName(methodCallExpression);
                return name.Substring(expression.Parameters[0].Name.Length + 1);
    
            }
            return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
        }
    
        private static string GetInputName(MethodCallExpression expression)
        {
            // p => p.Foo.Bar().Baz.ToString() => p.Foo OR throw...
    
            var methodCallExpression = expression.Object as MethodCallExpression;
            if (methodCallExpression != null)
            {
                return GetInputName(methodCallExpression);
            }
            return expression.Object.ToString();
        }
    
        public static string RadioButton(this HtmlHelper htmlHelper, string name, SelectListItem listItem,
                             IDictionary<string, object> htmlAttributes)
        {
            var inputIdSb = new StringBuilder();
            inputIdSb.Append(name)
                .Append("_")
                .Append(listItem.Value);
    
            var sb = new StringBuilder();
    
            var builder = new TagBuilder("input");
            if (listItem.Selected) builder.MergeAttribute("checked", "checked");
            builder.MergeAttribute("type", "radio");
            builder.MergeAttribute("value", listItem.Value);
            builder.MergeAttribute("id", inputIdSb.ToString());
            builder.MergeAttribute("name", name + ".SelectedValue");
            builder.MergeAttributes(htmlAttributes);
            sb.Append(builder.ToString(TagRenderMode.SelfClosing));
            sb.Append(RadioButtonLabel(inputIdSb.ToString(), listItem.Text, htmlAttributes));
            sb.Append("<br>");
    
            return sb.ToString();
        }
    
        public static string RadioButtonLabel(string inputId, string displayText,
                                     IDictionary<string, object> htmlAttributes)
        {
            var labelBuilder = new TagBuilder("label");
            labelBuilder.MergeAttribute("for", inputId);
            labelBuilder.MergeAttributes(htmlAttributes);
            labelBuilder.InnerHtml = displayText;
    
            return labelBuilder.ToString(TagRenderMode.Normal);
        }
    
    
        public static TProperty GetValue<TModel, TProperty>(HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
        {
            TModel model = htmlHelper.ViewData.Model;
            if (model == null)
            {
                return default(TProperty);
            }
            Func<TModel, TProperty> func = expression.Compile();
            return func(model);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
Is there a HTML Viewer for FireMonkey like this (VCL): http://code.google.com/p/thtmlviewer/downloads/list ? It will
I have aps.net web form there is html input text: <input type=text class=input runat=server
The system I'm using has a bug dealing with ' ,is there a html
Is there any HTML code analyzing tool that suggests tips to improve the HTML
I added this polygon code from this website: http://www.geocodezip.com/v3_polygon_example_donut.html There is an issue though,
is there an Html decode methode with prototype? I want to translate html code
I followed this link http://www.phpunit.de/manual/3.6/en/database.html there i found some useful details of php db
I am generating an ASP.NET Radio button list control in my server side code.I
Is there an HTML color wheel that I can throw in my site that

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.