0

Perfectly normal radio buttons in HTML look like this:

<input type="radio" name="somename" value="1">First</input>
<input type="radio" name="somename" value="2">Second</input>
<input type="radio" name="somename" value="3">Third</input>

As far as I can see, there is no way to set the "InnerHTML" (to use a JavaScript term) in an HTML helper; something like:

<%: Html.RadioButtonFor(m => m.somename, 1, "First") %>

All of the examples I've seen create a separate label for each button, which could work, but really screws up the HTML. Can this be done, and if not, why?

Scott Baker
  • 10,013
  • 17
  • 56
  • 102
  • Far from screwing up the HTML, labels are very helpful in terms of accessibility for all input controls, even radio buttons. – Xhalent May 26 '11 at 20:32
  • @Xhalent: I realize this is my personal opinion, but text seems far more useful, especially when using javascript to access the innerhtml. There is a reason inputs have a text element. – Scott Baker May 26 '11 at 20:36
  • @Xhalent: Well, I'm an ass - I did some further checking and in HTML there is no closing tag for the element. I'll shut up and make with the labels now. :-) – Scott Baker May 26 '11 at 20:53

2 Answers2

0

You can make your own html helper:

public static MvcHtmlString RadioButtonFor<TModel, TProperty>( this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object value, string innerHtml )
{
    var memberExpression = (MemberExpression)expression.Body;
    var name = memberExpression.Member.Name;
    var radioButton = string.Format( "<input type='radio' name='{0}' value='{1}'>{2}</input>", name, value, innerHtml );
    return MvcHtmlString.Create( radioButton );
}
Luuk
  • 1,959
  • 1
  • 21
  • 43
  • I'm completely wrong - in HTML there is no closing tag for elements. Thanks for the HtmlHelper implementation, tho. – Scott Baker May 26 '11 at 20:54
0

In HTML there is no closing tag for <input> elements. A well-formed input element has no children.

Scott Baker
  • 10,013
  • 17
  • 56
  • 102