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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:30:07+00:00 2026-06-07T19:30:07+00:00

I need some help. I’m trying to build a view where I need groups

  • 0

I need some help. I’m trying to build a view where I need groups of radiobuttons of enum types.
I have several enum types(classes) like this:

[DataContract(Namespace = Constants.SomeDataContractNamespace)]
public enum OneEnumDataContract
{
    [Display(Name = "Text_None", Description = "Text_None", ResourceType = typeof(TextResource))]
    [EnumMember]
    None = 0,

    [Display(Name = "Text_Medium", Description = "Text_Medium", ResourceType = typeof(TextResource))]
    [EnumMember]
    Medium = 1,

    [Display(Name = "Text_Very", Description = "Text_Very", ResourceType = typeof(TextResource))]
    [EnumMember]
    Very = 2
}

In my model(a datacontract, using WCF) I have this property for the enum datacontract:

    [DataMember(Order = 23)]
    [Display(Name = "EnumValue", Description = "EnumValue_Description", ResourceType = typeof(TextResource))]
    public OneEnumDataContract EnumClass1 { get; set; }

In my view I would try to make the group of radiobuttons like this(with a helper):

@Html.RadioButtonListEnum("EnumList1", Model.EnumClass1)

My helper:

public static MvcHtmlString RadioButtonListEnum<TModel>(this HtmlHelper<TModel> helper, string  NameOfList, object RadioOptions)
    {
        StringBuilder sb = new StringBuilder();
        //som other code for pairing with resourcefile...

        foreach(var myOption in enumTexts.AllKeys)
        {
            sb.Append("<p>");
            sb.Append(enumTexts.GetValues(myOption)[0]);
            sb.Append(helper.RadioButton(NameOfList, System.Convert.ToInt16(myOption)));
            sb.Append("</p>");
        }
        return MvcHtmlString.Create(sb.ToString());
    }

This gives me the first enumvalue in OneEnumDataContract, None, as the parameter RadioOptions.
How can I get all the enumvalues in the datacontract into the helper?

  • 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-07T19:30:09+00:00Added an answer on June 7, 2026 at 7:30 pm

    This is one I created recently. It won’t work if you try it on a non-enum but works for my enum needs. I copied bit’s and pieces from different DropDownList helpers like nikeaa posted.

    #region RadioButtonList
    
    
    public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes = null) where TModel : class
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        String field = ExpressionHelper.GetExpressionText(expression);
        String fieldname = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(field);
        var inputName = fieldname;
        TProperty val = GetValue(htmlHelper, expression);
    
        var divTag = new TagBuilder("div");
        divTag.MergeAttribute("id", inputName);
        divTag.MergeAttribute("class", "radio");
        foreach (var item in Enum.GetValues(val.GetType()))
        {
    
    
            DisplayAttribute[] attr = (DisplayAttribute[])item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DisplayAttribute), true);
            if (attr == null || attr.Length == 0 || attr[0].Name != null)
            {
                string name = attr != null && attr.Length > 0 && !string.IsNullOrWhiteSpace(attr[0].Name) ? attr[0].Name : item.ToString();
                var itemval = item;
                var radioButtonTag = RadioButton(htmlHelper, inputName, new SelectListItem { Text = name, Value = itemval.ToString(), Selected = val.Equals(itemval) }, htmlAttributes);
    
                divTag.InnerHtml += radioButtonTag;
            }
        }
    
    
        return new MvcHtmlString(divTag.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);
        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);
    }
    
    #endregion
    

    I use it like this

    @Html.RadioButtonListFor(m => m.PlayFormat)
    

    You may need to more code to set the correct element name for more complicated uses.

    If the enum items have a Display attribute, the name is displayed. Otherwise the enum item is displayed. If the Display name is null, that value is not shown as an option. In this enum, “None” isn’t displayed, “Singles” is displayed from the enum value, “Men’s Doubles” and all the other’s have text from [Display(Name=”Men’s Doubles”)]

    public enum PlayFormat
    {
        [Display(Name=null)]
        None = 0,
        Singles = 1,
        [Display(Name = "Men's Doubles")]
        MenDoubles = 2,
        [Display(Name = "Women's Doubles")]
        WomenDoubles = 3,
        [Display(Name = "Mixed Doubles")]
        MixedDoubles = 4,
        [Display(Name = "Men's Group")]
        MenGroup = 5,
        [Display(Name = "Women's Group")]
        WomenGroup = 6,
        [Display(Name = "Mixed Group")]
        MixedGroup = 7
    }
    

    The page looks like this (except each – is a radio button)

    - Singles
    - Men's Doubles
    - Women's Doubles
    - Mixed Doubles
    - Men's Group
    - Women's Group
    - Mixed Group
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help joining these two tables I have two views that looks like
Need some help. I have a table with some columns..like name , phone etc...
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help with a query.. I have three tables. Source id name 1
Need some help with DataFormatString in GridView. I have a Double value that needs
Need some help here. I have deployed spree (0.70.3) on slicehost (ubuntu, ruby1.8.7, Rails
Need some help with below issue We have 2 machines, each of these machines

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.