0

I have a checkbox in .cshtml page;

@Html.CheckBox("All", new { @class = "chkBox"})

I want to get the boolean value of this checkbox in my controller class, for that I have tried

bool all = Convert.ToBoolean(collection["All"]);

where collection is the object of FormCollection Class.
But I am getting value of the checkbox as "all", I don't know how to get the checked or unchecked value using formcollection object.

If anyone have any Idea then please tell me. Thanks.

cincodenada
  • 2,877
  • 25
  • 35
Learner
  • 776
  • 6
  • 14
  • 40

6 Answers6

2

Why not bind it to a model?

public class MyModel {
    public bool All {get; set;}
}

In your view just do the following

@html.CheckBoxFor(m=>m.All)

That should do it.

Gjohn
  • 1,261
  • 1
  • 8
  • 12
2

You Can Get that In this way also:

Let us assume your controller name is Index

       [HttpPost]
        public ActionResult Index(bool All)
        {
            return View();
        }

If "All"(Checkbox) is Checked then All becomes true.

If "All"(Checkbox) is UNchecked then All becomes false.

Based on true or false You can modify your code in your way.

Note:The variable you mentioned in Post Controller Must be same as the name which you given for checkbox in cshtml.

i.e;

@Html.CheckBox("All", new { @class = "chkBox" })

public ActionResult Index(bool All)

Ajay
  • 2,022
  • 19
  • 33
1

That's because you input tag has the value as all. In this case, it's a good pratice to bind the right type, for sample:

public ActionResult Post(bool all)
{
   //here you will get the all boolean value, if check or not as boolean
}

Or, better than this, you could create a ViewModel and bind it, for sample:

Add a class wih the fields you want to use on the Models folder.

public class MyViewModel
{
    public string Name { get; set; } 
    public bool All { get; set; }
}

After it, in your View, you just type the View and create the controls using the Html helpers, for sample:

@model Models.MyViewModel

@using (Html.BeginForm("Post", "Home", FormMethod.Post))
{
    @Html.TextBoxFor(model => model.Name)

    @Html.CheckBoxFor(model => model.All)

    <input type="submit" value="Send" />
}

Obs: it's just a sample

And in your controller, you just get a instance of this model, and let asp.net mvc bind it for you as the object:

public ActionResult Post(MyViewModel model)
{
    // here you have the model.All as boolean value

}

It's a good pratice to do, because ViewModels represents the fields you need on the View and transfer it between contexts (controllers, views, services, apis, etc..).

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
0

You can try this approach. You can get the more information from here asp.net mvc: why is Html.CheckBox generating an additional hidden input

    bool all = (collections["all"] != "false");
Community
  • 1
  • 1
Devesh
  • 4,500
  • 1
  • 17
  • 28
0

You need to modify your code like this. Its working: you need to first convert it in string then convert to boolean.

 Convert.ToBoolean(collection["All"].ToString());
RKT
  • 114
  • 5
0

Thanks Guys for all of your answers, all make sense. But in accordance of achieving the above requirement I have modified my .cshtml code a bit, rather then using razor syntax I am using this in my .cshtml page

<input id="All" name="All" value="true" type="checkbox" class="chkBox" />

Now in my .cs page I have harvested the boolean value as follows;

all = Convert.ToBoolean(collection["All"]);

So, basically what it does is, if the checkbox is left unchecked then it will give value as false otherwise, formcollection will take into account the checkbox and will give the value of all as true.

Learner
  • 776
  • 6
  • 14
  • 40