tags:

views:

203

answers:

5

Hi

Can anyone recommend a good book or tutorial for learning about the HtmlHelper class, specifically methods Html.CheckBox() and Html.Radio()?

+2  A: 

What? Helpers CheckBox and RadioButton are included in asp.net mvc 1.0. If you want to look how are made, then you can download the source code from the codeplex.

Edit: You can also take a look at this tutorial which explains how to create helpers.

rrejc
A: 

From the list of books to a broader question I would recommend the Mvc in Action.

dove
A: 

If you really want something:

public static string Radio(this HtmlHelper html, string group, string value)
{
  return string.Format("<input type='radio' name='{0}' value='{1}' />", group, value);
}

public static string Checkbox(this HtmlHelper html, string group, string value)
{
  return string.Format("<input type='checkbox' name='{0}' value='{1}' />", group, value);
}

This is an extremely basic way of doing it, and provides no way over providing additional attributes, such as id, class, title, or anything else. For further reading on this, look at Rob Conery's blog post on this. Note that, although it's for MVC v1.0 preview, most of it should still apply.

Besides... As has been mentioned, please follow the links in the other answers by rrejc and dove to give you a better idea.

Dan Atkinson
+2  A: 

Hope this information will help you....I did enough R&D on the checkbox which creates additional html control.

Always use Html.CheckBox() overload methods for creating checkbox controls.

Notice that the check box helper (Html.CheckBox()) renders two input controls. First, it renders a check box control as you’d expect, and then it renders a hidden input control of the same name. This is to work around the fact that when check boxes are deselected, browsers don’t submit any value for them. Having the hidden input control means the MVC Framework will receive the hidden field’s value (i.e., false) when the check box is unchecked.

From : Apress.Pro.ASP.NET.MVC.Framework.

Checkbox doesn't have a label. So add the html label tag with for attribute with value as “name of checkbox”

<%= Html.CheckBox("chkAccept1")%><label for="chkAccept1">Accept Terms & Conditions</label>
<input id="chkAccept1" name="chkAccept1" type="checkbox" value="true" /><input name="chkAccept1" type="hidden" value="false" /><label for="chkAccept1">Accept Terms & Conditions</label> 

        <%= Html.CheckBox("chkAccept2", true )%><label for="chkAccept2">Accept Terms & Conditions</label>
 <input checked="checked" id="chkAccept2" name="chkAccept2" type="checkbox" value="true" /><input name="chkAccept2" type="hidden" value="false" /><label for="chkAccept2">Accept Terms & Conditions</label> 

        <%= Html.CheckBox("chkAccept3", false, new { @class="error", @client_selector="alphanumeric" }) %><label
            for="chkAccept3">Accept Terms & Conditions</label>
<input class="error" client_selector="alphanumeric" id="chkAccept3" name="chkAccept3" type="checkbox" value="true" /><input name="chkAccept3" type="hidden" value="false" /><label for="chkAccept3">Accept Terms & Conditions</label>
Gabriel Susai
A: 

I would recommend FluentHtml, XForms, or InputBuilder instead. Or even better, MVC v2 templated inputs which I can't wait for.

queen3