How to get checkbox value from FormCollection?

被刻印的时光 ゝ 提交于 2019-12-19 03:41:42

问题


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?


回答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




回答2:


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);



回答3:


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);



回答4:


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.




回答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
    ...
}


来源:https://stackoverflow.com/questions/28987105/how-to-get-checkbox-value-from-formcollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!