Click here to Skip to main content
15,868,024 members
Articles / Web Development / HTML

Under the Hood of @Html.Checkbox()

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
31 Oct 2012CPOL2 min read 12.7K   1   3
Under the hood of @Html.Checkbox()

Yesterday, I used checkbox helper in my MVC project and found some interesting things when I saw render HTML of checkbox helper and access value of checkbox from form collection in controller action. I have described both the things below.

1. CheckBox Helper Renders 2 Input Elements

The CheckBox helper is different from the other controls because it will render two input elements. I have tried with the following code in a view.

HTML
@Html.Checkbox(‘chkAcceptPolicy’)

And check this code in browser source view, it will render the following HTML.

HTML
<input id="chkAcceptPolicy" name="chkAcceptPolicy" type="checkbox" value="true"/>
<input  name="chkAcceptPolicy" type="hidden" value="false"/>

I have probably wondered why the helper renders a hidden input in addition to checkbox input. After thinking over that, I have found reason for that is the HTML Specification indicates that a browser will submit a value for a checkbox only when checkbox is checked. So as per the above example that if chkAcceptPolicy checkbox value is not checked, then second input guarantees a value will appear.

2. Form Collection Returns Value of Both Input Elements

If we used model binding approach, then it will return value true if value of checkbox is checked, otherwise return false.

But here, I have tried to access checkbox value from the Form Collection or Request Object.

C#
[HttpPost]
public ActionResult Index(FormCollection fB)
{
    ViewBag.ValueofCheckBox = fB["chkAccpetPolicy"];
    return View();
}

Here, I found some interesting things. If you checked value of checkbox at that time, you will get the value of checkbox from the form collection object as "true,false".

Why it will contain both values because as per HTML Specifications, a browser will submit a value with comma separated when both elements have the same name.

So here, chkAcceptPolicy checkbox value is checked and hidden element value is false and due to this formcollection, returns value as ‘true,false’.

The goal of this article is just to show you a unique thing about @html.checkbox. Hope this will help you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I have been working as a Software Engineer on Microsoft .NET Technology.I have developed several web/desktop application build on .NET technology .My point of interest is Web Development,Desktop Development,Ajax,Json,Jquey,XML etc.I have completed Master of Computer Application in May-2011.I'm not happy unless I'm learning something new.

Comments and Discussions

 
QuestionTwo checkboxes? Pin
Ryan Criddle30-Oct-12 15:12
Ryan Criddle30-Oct-12 15:12 
AnswerRe: Two checkboxes? Pin
Jigar Bagadai30-Oct-12 21:08
Jigar Bagadai30-Oct-12 21:08 
GeneralMy vote of 5 Pin
Immortal Genious30-Oct-12 6:27
Immortal Genious30-Oct-12 6:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.