How to use DataAnnotations Display attribute for multiple radio button lables

152 views Asked by At

It's handy to use a Display attribute for model properties in MVC:

Model:

[Display(Name="Your Name:")]
public string Name { get; set; }

View:

@Html.LabelFor(m => m.Name)
@Html.EditorFor(m => m.Name)

But ... is it possible to use the Display attribute for naming the individual choices for radio buttons? The following is what I use now, but the 'Label for...' tag is a little inconsistent with the rest of the view. Anyone?

<div class="radio-inline">
    @Html.RadioButtonFor(m => m.OpenToPublic, true, new { id = "isOpenToPublic" })
    <label for="isOpenToPublic">Open to the Public</label>
</div>
<div class="radio-inline">
    @Html.RadioButtonFor(m => m.OpenToPublic, false, new { id = "isInviteesOnly" })
    <label for="isInviteesOnly">Invitees Only</label>
</div>

I like the code above, since using the Label tag results in being able to click on the label text to select the radio button, and the styling of the text is correct. I just wonder if there's a way to do this with data annotations on the model's property for the radio button.

Thanks, Brian

0

There are 0 answers