Click here to Skip to main content
12,400,972 members (63,494 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.9K views
1 bookmarked
Posted

Under the hood of @Html.Checkbox()

, 31 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
CodeProject Yesterday, I have used checkbox helper in my mvc project and found some interesting thing 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.

Yesterday, I have used checkbox helper in my mvc project and found some interesting thing 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 element

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

@Html.Checkbox(‘chkAcceptPolicy’)

And check this code in browser source view it will render following html.

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

I have probably wondering why the helper renders a hidden input in addition to checkbox input. After thinking over that I have found reason for that the HTML Specification indicates that a browser will submit a value for a checkbox only when checkbox is checked. So as per 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 element

If we are 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.

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

Here I am found some interesting thing. If you are checked value of checkbox at that time you are getting value of checkbox from the form collection object as "true,false".

Why it will contains both value because as per HTML Specifications that a browser will submit a value with comma separated when both element have name same.  

So here, chkAcceptPolicy checkbox value is checked and hidden element value is false ndue to this formcollection return value a ‘true,false’.

The goal of this article just show you 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)

Share

About the Author

Jigar Bagadai
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.

You may also be interested in...

Comments and Discussions

 
QuestionTwo checkboxes? Pin
Ryan Criddle30-Oct-12 15:12
memberRyan Criddle30-Oct-12 15:12 
AnswerRe: Two checkboxes? Pin
Jigar Bagadai30-Oct-12 21:08
memberJigar Bagadai30-Oct-12 21:08 
GeneralMy vote of 5 Pin
Hirun Meepage30-Oct-12 6:27
memberHirun Meepage30-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 31 Oct 2012
Article Copyright 2012 by Jigar Bagadai
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid