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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:03:30+00:00 2026-05-27T05:03:30+00:00

I have the following model: public class Person { public string Name { get;

  • 0

I have the following model:

public class Person
{
    public string Name { get; set; }
    public string Gender { get; set; }
}

I want the default gender to be female, so I set that up in the action:

public ActionResult Create()
{
    var model = new Person { Gender = "F" }; // default is female
    return View(model);
}

Finally, the view renders everything like this:

@model Person

@using (Html.BeginForm())
{
    @Html.EditorForModel()
    <p><input type="submit" value="Create" /></p>
}

This all works as expected. Now, let’s say that instead of a simple textbox, I want to display the gender as a more intuitive pair of radio buttons. So I make the following template and save it to Shared/EditorTemplates/Gender.cshtml:

@model string
@Html.RadioButtonFor(model => model, "F") Female
@Html.RadioButtonFor(model => model, "M") Male

Finally I decorate Gender with [UIHint("Gender")].

Gender is now properly rendered with radio buttons, which is great, BUT…

The problem

Female no longer comes pre selected as the default value and instead I end up with the two radio buttons blank. Am I missing something?

Interestingly, if I move RadioButtonFor from the template to the view (changing model => model to model => model.Gender), then everything works as expected. I know this is a viable workaround, but these templated helpers are such an amazing, addictive convenience that I would rather exhaust all possibilities before letting them go.

  • 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-27T05:03:31+00:00Added an answer on May 27, 2026 at 5:03 am

    I know it’s ugly but the following might work:

    @model string
    @Html.RadioButton("", "F", Model == "F") Female
    @Html.RadioButton("", "M", Model == "M") Male
    

    The problem with the RadioButton helper is that if the first argument is null or empty it will always consider it as non checked thus making this helper inappropriate for editor templates.

    Here’s an excerpt from the MVC source code which illustrates what I mean:

    bool isChecked = !string.IsNullOrEmpty(name) && string.Equals(htmlHelper.EvalString(name), b, StringComparison.OrdinalIgnoreCase);
    

    As an alternative you could use a custom HTML helper to generate a list of radio buttons.

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

Sidebar

Related Questions

I have the following Model: public class Person { public string Name {get;set;} public
So I have the following model: public class Person { public String FirstName {
Given the classes: public class Person { public string Name { get; set; }
I have following model: public class FormularModel { [Required] public string Position { get;
I have the following model: public class A { public int? Id {get;set;} public
I have the following domain model: public class Name { private readonly string fullName;
ok say i have the following model: public class bar{ public string bar {get;
I have a custom class: public class Person { public String Name { get;
I have the following model public class UserViewModel { public String CVR_Nummer { get;
I have the following model Person in my UI MVC layer: public class Person

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.