How to get checkbox value from FormCollection?

I have a checkbox and button:

@using(Html.BeginForm())
{
    <div id="search-block">
        <input type="checkbox" name="showAll" id="showAll">Include completed
        <input type="submit" value="Apply"/>
    </div>
}

and second thing in controller:

[HttpPost]
public ActionResult Index(FormCollection collection)
{
    var showAll = collection["showAll"];
    TempData["showAll"] = showAll;

    ... 
    something
    ...
}

It’s actually working, BUT:

If checkboxes are not checked, I am receiving null (doesn’t bother me much).

If checkboxes are checked, I am receiving “on” from FormCollection, and this is not what I need. I want to receive true or false.

How can I do this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

<input type="checkbox" name="showAll" id="showAll">Include completed

Will Post a value of "on" or "off".

This WONT bind to a boolean,

To receive a boolean value you can use HTML Helper Checkbox
Like

@Html.CheckBox("showAll")
<label for="showAll" style="display:inline">Include completed</label>

Update:

If checked the html helper will post back "true,false" and if
unchecked as "false"

You can have this workaround as

bool MyBoolValue= Convert.ToBoolean(collection["showAll"].Split(',')[0]);

This is because the Html helpers class adds an extra hidden field to save the state of the checkbox

Method 2

I have tried out with input tag and it works as shown below:

<form method="post">
    <input type="checkbox" id="chkRmb" name="chkRmb" /> Remember Me?
    <input type="submit" value="Log In" class="btn btn-default login-btn" />
</form>

In the controller, compare the string value and convert its to a boolean.

bool rmbMe = (formdata["chkRmb"] ?? "").Equals("on", StringComparison.CurrentCultureIgnoreCase);

Method 3

So I gave this a shot and it seemed happy to take it, where collection is my FormCollection object:

if (collection.AllKeys.Contains("Inactive"))
{
     np.Inactive = false;
}
else
{
    np.Inactive = true;
}

Please be advised that the name of the targeted element is confusing – but this was the intended result I was seeking.

Method 4

I had trouble getting this to work and this is the solution I came up with. I’m showing it with an html helper.

@Html.CheckBoxFor(model => model.showAll, new { @class = "form-control", Name = "showAll"})

This is where I kept falling down and the following works.

bool MyBoolValue= (collection["showAll"] ?? "").Equals("true", StringComparison.CurrentCultureIgnoreCase);

Method 5

Try this, it solved this same issue for me. Thought I would share it in case others came upon this issue in the future.

[HttpPost]
public ActionResult Index(FormCollection collection)
{
    Boolean tempValue = collection["showAll"] != null ? true : false;
    TempData["showAll"] = tempValue;

    ... 
    something
    ...
}

Method 6

Try this

bool MyBoolValue= collection["showAll"].Count > 1;

when 1 then false

when 2 then true


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x