clement
clement

Reputation: 4266

RadioButtonFor and label

I'm using RadioButtonFor in my web project, but I want to use the label in order to select the item if the user selects the text of the radio button, but the html generated gives me multiple same #id's because of the Radiobuton and I want to change this in order to have clear html and use jquery selectors without troubles (with the unique id):

CSHTML

@foreach (var item in Model.audit.natureSelect)
{
    <label>
        @Html.RadioButtonFor(m => m.audit.nature, item.Id, new { Value = item.Id }) @item.NatAudit
    </label>
}

HTML

<div class="col-md-10">
    <label><input id="audit_type" name="audit.type" type="radio" value="1">item 1</label> 
    <label><input id="audit_type" name="audit.type" type="radio" value="2">item 2</label> 
    <label><input id="audit_type" name="audit.type" type="radio"value="3">item 3</label>
</div>

Thanks to help me to make it clean

Upvotes: 1

Views: 2220

Answers (1)

ataravati
ataravati

Reputation: 9155

You are not using the RadioButtonFor helper correctly. What you need is for all the radio buttons to have a single name, but different ids. And, here's how you do it:

@foreach (var item in Model.audit.natureSelect)
{
    <label>
        @Html.RadioButtonFor(m => m.audit.nature, item.Id, new { id = item.Id }) @item.NatAudit
    </label>
}

By the way, follow C# naming conventions when naming your properties and methods. Property names in C# should start with a capital letter.

Upvotes: 5

Related Questions