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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:36:55+00:00 2026-06-16T16:36:55+00:00

Problem: Need to bind a strongly typed model which has a Gender as enum

  • 0

Problem:

Need to bind a strongly typed model which has a Gender as enum property. Also i like to show a Display text from a Resource file.

My Model is

public enum GenderViewModel
{
  [Display(Name = "Male", ResourceType = typeof(Resources.Global), Order = 0)]
  Male,
  [Display(Name = "Female", ResourceType = typeof(Resources.Global), Order = 1)]
  Female

}

Initially, I tried following http://romikoderbynew.com/2012/02/23/asp-net-mvc-rendering-enum-dropdownlists-radio-buttons-and-listboxes/

But it was bit complex and i was unable to correct my HTML however i want.

Then i had a look of simple and easy implementation from stackoverflow, pass enum to html.radiobuttonfor MVC3

and used a HtmlHelper in cshtml like below

@Html.RadioButtonForEnum(m => m.Gender)

HTML Produced

<label for="_Gender_Male">
 <input type="radio" value="Male" name="Gender" id="_Gender_Male" 
 data-val-required="Gender is required" data-val="true" checked="checked">
 <span class="radiotext">Male</span>
</label>

<label for="_Gender_Female">
<input type="radio" value="Female" name="Gender" id="_Gender_Female">
<span class="radiotext">Female</span></label>

It really simple and works well for me. But i am not getting values
from Resource files. My application is multilingual and I use a Global
Resource file for different language support.

Issue:

Male displayed should be Man and Female displayed should be Kvinna should be from Resource file, as my current culture is sv-se

Could any one help/ provide a simple solution which has a good control over HTML?

  • 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-16T16:36:56+00:00Added an answer on June 16, 2026 at 4:36 pm

    All you have to do is adapt my original helper so that it takes into account the DisplayAttribute:

    public static class HtmlExtensions
    {
        public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression
        )
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            if (!metaData.ModelType.IsEnum)
            {
                throw new ArgumentException("This helper is intended to be used with enum types");
            }
    
            var names = Enum.GetNames(metaData.ModelType);
            var sb = new StringBuilder();
    
            var fields = metaData.ModelType.GetFields(
                BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public
            );
    
            foreach (var name in names)
            {
                var id = string.Format(
                    "{0}_{1}_{2}",
                    htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
                    metaData.PropertyName,
                    name
                );
                var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
                var field = fields.Single(f => f.Name == name);
                var label = name;
                var display = field
                    .GetCustomAttributes(typeof(DisplayAttribute), false)
                    .OfType<DisplayAttribute>()
                    .FirstOrDefault();
                if (display != null)
                {
                    label = display.GetName();
                }
    
                sb.AppendFormat(
                    "<label for=\"{0}\">{1}</label> {2}",
                    id,
                    HttpUtility.HtmlEncode(label),
                    radio
                );
            }
            return MvcHtmlString.Create(sb.ToString());
        }
    }
    

    Now if you have decorated some of the enum values with the DisplayAttribute, the values will come from the resource file.

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

Sidebar

Related Questions

Friends I have a problem We need to make a user control that has
I think I need to bind data programmatically to solve my problem. I use
I need to bind a property to an edit control and have the control
I need to dynamically bind() the click event, but the problem is when I
Working on a website that is also viewable on mobile and need to bind
Goal: Gain datatype date with year and month Problem: Need to help to convert
Problem: I need to write/read objects from a file.This because I need to write/read
The problem: I need to ensure that a particular directory hierarchy exists in the
My problem : I need to create draggable widgets (here it's a jslider for
To solve some problem I need to compute a variant of the pascal's triangle

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.