a developer's notes – a semi-technical web development BLOG

March 8, 2014

Checkbox returning “true,false” from FormCollection.

Filed under: ASP.NET MVC — Duy Nguyen @ 10:46 am
Tags: , , , , , , , , , ,

If you try Request.Form[“checkbox0”], you’ll get the value “true,false” .

Just check this:

form[key].Contains("true"); //some said chrome returns "on"

Or

You could simplify your code doing this:

var strArray = collection["YourCheckboxName"];
char[] delimiterChars = {‘,’};
string[] checkedvalue = strArray.Split(delimiterChars);
var returnValue = checkedValue.Length > 1;

If the checkbox is checked, then the array will contain 2 values, if not, only one.

Advertisements

3 Comments »

  1. You can easily put a “value” attribute to the checkbox and check if the (collection[“YourCheckboxName”] == ) 😀

    Comment by Mohamed Fekry — October 8, 2014 @ 11:33 am | Reply

  2. sorry, the comment isn’t complete
    check if (collection[“YourCheckboxName”] == value attribute)

    Comment by Mohamed Fekry — October 8, 2014 @ 11:36 am | Reply

  3. Thank you so much..
    -Anu

    Comment by Anonymous — November 13, 2014 @ 2:00 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: