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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:50:24+00:00 2026-05-27T21:50:24+00:00

I have this Razor Template <table> <tr> <td>@Html.RadioButtonFor(i => i.Value, 1)</td> <td>@Html.LabelFor(i => i.Value,

  • 0

I have this Razor Template

<table>
<tr>
    <td>@Html.RadioButtonFor(i => i.Value, "1")</td>
    <td>@Html.LabelFor(i => i.Value, "true")</td>
</tr>
<tr>
    <td>@Html.RadioButtonFor(i => i.Value, "0")</td>
    <td>@Html.LabelFor(i => i.Value, "false")</td>
</tr>
</table>

That gives me this HTML

<table>
<tr>
    <td><input id="Items_1__Value" name="Items[1].Value" type="radio" value="1" /></td>
    <td><label for="Items_1__Value">true</label></td>
</tr>
<tr>
    <td><input checked="checked" id="Items_1__Value" name="Items[1].Value" type="radio" value="0" /></td>
    <td><label for="Items_1__Value">false</label></td>
</tr>
</table>

So I have the ID Items_1__Value twice which is – of course – not good and does not work in a browser when I click on the second label “false” the first radio will be activated.

I know I could add an own Id at RadioButtonFor and refer to that with my label, but that’s not pretty good, is it? Especially because I’m in a loop and cannot just use the name “value” with an added number, that would be end up in multiple Dom Ids in my final HTML markup as well.

Shouldn’t be a good solution for this?

  • 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-27T21:50:24+00:00Added an answer on May 27, 2026 at 9:50 pm

    I’ve been wondering how MVC determines “nested” field names and IDs. It took a bit of research into the MVC source code to figure out, but I think I have a good solution for you.

    How EditorTemplates and DisplayTemplates determine field names and IDs

    With the introduction of EditorTemplates and DisplayTemplates, the MVC framework added ViewData.TemplateInfo that contains, among other things, the current “field prefix”, such as "Items[1].". Nested templates use this to create unique names and IDs.

    Create our own unique IDs:

    The TemplateInfo class contains an interesting method, GetFullHtmlFieldId. We can use this to create our own unique IDs like so:

    @{string id = ViewData.TemplateInfo.GetFullHtmlFieldId("fieldName");}
    @* This will result in something like "Items_1__fieldName" *@
    

    For The Win

    Here’s how to achieve the correct behavior for your example:

    <table>
    <tr>
        @{string id = ViewData.TemplateInfo.GetFullHtmlFieldId("radioTrue");}
        <td>@Html.RadioButtonFor(i => i.Value, "1", new{id})</td>
        <td>@Html.LabelFor(i => i.Value, "true", new{@for=id})</td>
    </tr>
    <tr>
        @{id = ViewData.TemplateInfo.GetFullHtmlFieldId("radioFalse");}
        <td>@Html.RadioButtonFor(i => i.Value, "0", new{id})</td>
        <td>@Html.LabelFor(i => i.Value, "false", new{@for=id})</td>
    </tr>
    </table>
    

    Which will give you the following HTML:

    <table>
    <tr>
        <td><input id="Items_1__radioTrue" name="Items[1].Value" type="radio" value="1" /></td>
        <td><label for="Items_1__radioTrue">true</label></td>
    </tr>
    <tr>
        <td><input checked="checked" id="Items_1__radioFalse" name="Items[1].Value" type="radio" value="0" /></td>
        <td><label for="Items_1__radioFalse">false</label></td>
    </tr>
    </table>
    

    Disclaimer

    My Razor syntax is underdeveloped, so please let me know if this code has syntax errors.

    For what its worth

    It’s pretty unfortunate that this functionality isn’t built-in to RadioButtonFor. It seems logical that all rendered Radio Buttons should have an ID that is a combination of its name AND value, but that’s not the case — maybe because that would be different from all other Html helpers.
    Creating your own extension methods for this functionality seems like a logical choice, too. However, it might get tricky using the “expression syntax” … so I’d recommend overloading .RadioButton(name, value, ...) instead of RadioButtonFor(expression, ...). And you might want an overload for .Label(name, value) too.
    I hope that all made sense, because there’s a lot of “fill in the blanks” in that paragraph.

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

Sidebar

Related Questions

I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have this code in jQuery, that I want to reimplement with the prototype
I have this line in a useful Bash script that I haven't managed to
I have this code that is my navigation bar on my site, it is
I used to have something like this: We suggest you read our @Html.ActionLink(help page,
I have a razor display template in MVC3 for an address. Here is an
I have a class with this property [Display(Name = Estado Civil),UIHint(EstadoCivil),ScaffoldColumn(true)] public virtual EstadoCivil
I have a HtmlHelper that renders a grid as a table, in MVC2 I
I have written a few razor helpers and these helpers use functions that include
I have a webpage that i am converting to razor view engine. I have

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.