Custom styled radio buttons using ASP.NET MVC Helpers

Jordan Lewallen

I am struggling to style Html.RadioButtonFor() form elements in my ASP.NET MVC project. I'm trying to replicate something like this: http://jsfiddle.net/YB8UW/2374/

But with my HTML form section that looks like this:

<div class="form-group">
    <label>@Html.RadioButtonFor(m => m.Presave.IsTrack, true) Track</label>
    <label>@Html.RadioButtonFor(m => m.Presave.IsTrack, false) Album</label>
</div>

None of my styles are picked up when I do stuff like:

input[type="radio"]:checked + label {
    background:yellow;
}

The css for the label outline (taken from the fiddle) in my project works fine though

label {
    padding: 5px;
    border: 1px solid #CCC;
    cursor: pointer;
    z-index: 90;
}

but that's about it, I cannot get the background color style for the selected element. What additional syntax do I have to add for this to be picked up?

Thanks!

StaticBeagle

The + selector is looking for the label right after a checked radio and the following:

<div class="form-group">
    <label>@Html.RadioButtonFor(m => m.Presave.IsTrack, true) Track</label>
    <label>@Html.RadioButtonFor(m => m.Presave.IsTrack, false) Album</label>
</div>

will generate radio buttons that are inside labels e.g.

<div class="form-group">
    <label><input type="radio" />...</label>
    <label><input type="radio" />...</label>
</div>

Putting the labels right after the @RadioButtonFor should do the trick:

<div class="form-group">
    @Html.RadioButtonFor(m => m.Presave.IsTrack, true, new { id = "track" })
    <label for="track">Track</label>
    @Html.RadioButtonFor(m => m.Presave.IsTrack, false, new { id = "album" }) 
    <label for="album">Album</label>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Adding a custom class to Radio Buttons in ASP.NET MVC 4

Radio Button Tag Helpers in ASP.NET 5 MVC 6

Binding 4 Radio buttons using @html.RadioButtonFor MVC4 ASP.Net

ASP.NET MVC 5 group of radio buttons

Binding radio buttons in ASP.NET MVC page with razor

Input radio buttons in ASP.NET Core MVC with taghelpers

What is the advantage of using Tag Helpers in ASP.NET Core MVC

Should my custom ASP.Net 5 MVC 6 Tag Helpers have an asp- prefix?

Html helpers for enum in Asp .Net MVC

Radio Buttons with Styled Components for React

Using two different buttons on the controller in ASP.NET MVC

How to bind html controls without using ASP.Net MVC html helpers?

Remove auto Date Picker in chrome when using tag helpers in Asp.Net MVC core 2.2

ASP.NET MVC helpers, MVC version independent

Set HTML attributes on ASP.Net MVC 4 HTML Helpers?

Refactoring out default Asp.Net MVC helpers to their own file

How to add required field validation on client side with custom message for Radio Button in ASP.Net MVC?

ASP.NET MVC - Using Model in Custom HTML Helper

ASP.NET MVC 5.1 EditorFor and DisplayFor not Using Custom Templates

ASP.NET MVC Custom Principal not using ASPNETDB.MDF

Fluter - Custom Radio Buttons

Method 'RouteCollection.get_AppendTrailingSlash' not found when using Razor Url helpers in ASP.NET MVC 5 Mono

asp.net mvc changing buttons to text

Using custom Html helpers

ASP.NET Radio buttons selection to hide/show div section

three radio buttons were picked at the same time in asp.net

ASP.NET TreeView checkboxes behave as Radio Buttons

MVC: Custom, fluent Html Helpers

Custom label using vue and element UI library radio buttons