Getting Checkbox Value in ASP.NET MVC 4

Getting Checkbox Value in ASP.NET MVC 4

@Html.EditorFor(x => x.Remember)

Will generate:

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

How does it work:

  • If checkbox remains unchecked, the form submits only the hidden value (false)
  • If checked, then the form submits two fields (false and true) and MVC sets
    true for the model's bool property

<input id="Remember" name="Remember" type="checkbox" value="@Model.Remember" />

This will always send the default value, if checked.

Get checkbox values in controller mvc 4

assign a name to your checkbox:

<input name="gender" type="checkbox" id="@checkBoxId" class="chkclass" value="@names.Value" />

Then accept a string array of with parameter name gender

[HttpPost]
public ActionResult HandleFormSubmit(string[] gender,
MembershipFormViewModel model)
{
//model not valid, do not save, but return current umbraco page
if (ModelState.IsValid == false)
{
return CurrentUmbracoPage();
}
string test = "Gender: " + model.Gender + Environment.NewLine; //getting null here
return RedirectToCurrentUmbracoPage();
}

Handling Checkbox values in MVC 4

There are two issues here:

Firstly, your HTML inputs need a name attribute - not just an id - because it is the name which is used as the key in the form collection.

Secondly, they need a value. This is trickier, and is an age-old problem with checkboxes. By default, a browser will submit the value associated with a checkbox if it is checked, and will not submit anything if it is unchecked. Therefore, in order to make a checkbox submit true when checked and false when not checked, you need to do this:

<input type="checkbox" ID="chb_Active" name="chb_Active" value="true" style="color: #428bca;">
<input type="hidden" name="chb_Active" value="false">

(and similarly for your other checkbox, if you want to have two of them).

Using this setup, false will be posted back as the value of the chb_Active property of the form if the checkbox is not checked. If it is checked, then both false and true will be posted back, and ASP.NET will automatically pick out true for you if you use its model-binding feature, like so:

public ActionResult MethodName(string submit, bool chb_Active) {
// ...
}

How to get checkbox selected value and pass to controller in asp.net mvc 4

Based on the comments that an ajax post is not required, your AssignProductsViewModel requires an additional property to bind the checkboxes

public class AssignProductsViewModel
{
public long ProductID { get; set; }
public string ProductName { get; set; }
public bool IsSelected { get; set; } // add this
}

In the view, use a for loop or a custom EditorTemplate to render the collection do the controls are correctly named with indexers. A foreach loop generates duplicate id (invalid html) and name attributes (cannot be bound to a collection)

@model VehicleViewModel
@using(Html.BeginForm())
{
// controls for VehicleTypeName, LocationName
for(int i = 0; i < Model.AssignedProducts.Count; i++)
{
@Html.HiddenFor(m => m.AssignedProducts[i].ProductID) // ditto for ProductName if you want it on postback
@Html.CheckBoxFor(m => m.AssignedProducts[i].IsSelected)
@Html.LabelFor(m => m.AssignedProducts[i].IsSelected, Model.AssignedProducts[i].ProductName)
}
....
}

and post back to

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NewVehicle(VehicleViewModel model)
{
// model.AssignedProducts contains the collection with a value indicating if the product has been selected
}

Alternatively, you can use an EditorTemplate for type of AssignProductsViewModel to render the collection

In /Views/Shared/EditorTemplates/AssignProductsViewModel.cshtml

@model AssignProductsViewModel
@Html.HiddenFor(m => m.ProductID) // ditto for ProductName if you want it on postback
@Html.CheckBoxFor(m => m.IsSelected)
@Html.LabelFor(m => m..IsSelected, Model.ProductName)

and in the main view

@model VehicleViewModel
@using(Html.BeginForm())
{
// controls for VehicleTypeName, LocationName
@Html.EditorFor(m => m.AssignedProducts)
<input type="submit" />
}

Getting Checkbox Value in ASP.NET MVC Action

You are not setting the value attribute in either the checkbox or its associated hidden input so no value is posted and the value of your property is its default value (false). It needs to be

<label class="checkbox pull-left">
<input type="checkbox" id="IsActive" name="IsActive" value="true" ....>
<i></i>Active/Deactive
<input name="IsActive" type="hidden" value="false">
</label>

Side note: Is there any reason you're not using @Html.CheckBoxFor(m => m.IsActive) to generate the correct html?

Pass values of checkBox to controller action in asp.net mvc4

If a checkbox is checked, then the postback values will contain a key-value pair of the form [InputName]=[InputValue]

If a checkbox is not checked, then the posted form contains no reference to the checkbox at all.

Knowing this, the following will work:

In the markup code:

<input id="responsable" name="checkResp" value="true" type="checkbox" />

And your action method signature:

public ActionResult Index(string responsables, bool checkResp = false)
{
//Do Something
}

This will work because when the checkbox is checked, the postback will contain checkResp=true, and if the checkbox is not checked the parameter will default to false.

How to get value of Checkbox using formcollection in MVC4

I expect you required the below code. Please replace your code with this.

[HttpPost]
public string ExtractEmailId(FormCollection form)
{
var value = form["CkbQuestion1"];
return value;
}

And you view will be similar to

@Using(Html.Beginform("ExtractEmailId"))
{
<input type="checkbox" name="CkbQuestion1" />
<input type="submit" value="Submit" />

}

How to get the boolean value of checkbox in ASP.net MVC 4

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.



Related Topics



Leave a reply



Submit