0

Net MVC and I have a table with products I want to buy. I want to use a Checkbox to confirm the purchase.

But how can I insert the ID in a List<int>?

I have a class Approved with an attribute List<int> approve

I tried:

  • @Html.CheckBoxFor(m.approve.Add(id))
  • @Html.CheckBoxFor(m=>m.approve.Add(id))
  • @Html.CheckBoxFor(ViewBag.l.approve.Add(id)

How can that task be done ?

AnkurVyas
  • 259
  • 2
  • 12
  • you would need to use say JQuery to check to see if the user has checked the box and if so then inject into the DOM the collection and its items in the collection so when you POST it, the collection also gets POST'd to the controller or create the array when you are doing an AJAX POST. take a look here: http://stackoverflow.com/questions/15782417/post-javascript-array-with-ajax-to-asp-net-mvc-controller or here: http://stackoverflow.com/questions/6361078/posting-object-with-list-of-ints-from-jquery-to-net-mvc-3-controller – Ahmed ilyas Jul 14 '14 at 11:33
  • You could consider using a CheckedListBox helper. Unfortunately MVC doesn't have one out of the the box but take a look at [this example](http://www.codeproject.com/Tips/613785/How-to-Use-CheckBoxListFor-With-ASP-NET-MVC) –  Jul 14 '14 at 11:50

3 Answers3

0

You can easyly bind a list to a model:

So, you simply need to make a list of a class created for this, which includes the integer id, and a boolean to hold the checkbox state, somethign like this:

public class IdState
{
   public int Id {get;set;}
   public bool Checked {get;set;}
}

The list must be of this class, i.e. List<IdState>

The key is using a syntax like this:

Html.CheckBoxFor(m => m[i].Product)

Which will produce and input whose name is something like this:

name="[0].Product"

Then, the model binder will bind your list automatically. Of course, your post action must recevied a List<IdState> to function properly.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
0

u can use html to insert id to your checkbox

@model yourModel

//for generate checkbox
<input type="checkbox" @(yourmodel ? " checked=checked " : null) name="YourCheckBoxName" value="@model.ID" >

use formCollection for get value in your controller from your view

novian kristianto
  • 751
  • 3
  • 12
  • 34
0

I solved this problem that way:

This Javascript function walks through all checkboxes and saves the Id of the checked once to an hidden input.
<script> function submit() { var x = new Array(); for (i = 0; i < ids.length; i++){ if ($("#" + ids[i].toString() + ".cb").is(':checked')) { x.push(ids[i]); } } console.log(x.toString()); $("#str").val(x.toString()); } </script>

After toggling a checkbox the value of the hidden input is refreshed
<script> $(document).ready(function () { $("input.cb").change(function () { submit(); }); }); </script>

Here I create an array with all possible ids to improve simplicity and performance.
Otherwise I had to walk through all possible ids and check them.
<script>ids.push(@id);</script>

This is my checkbox:
<input type="checkbox" value="@id" id="@id" class="cb" />