I have an ASP.NET MVC 3 website - on a particular page I have used the datatables.net plugin for a table. I have included in my markup a checkbox to the left of the table that when clicked will highlight the row. The user could select multiple rows. When the user clicks the button I want to pass the IDs of all the row clicked back to my Controller to then go and do some work in a DB.
I want to pass back the ids in JSON format so it will be easy to deserialize the data in my controller.
So far this is were I have got too.
$('#UseCars').click(function (event) {
var cars = new Array();
carsTable.find("tr input:checkbox").each(function () {
var car = new Object();
//alert($(this).attr("CarID"));
car.ID = $(this).attr("CarID");
//alert(car.ID);
cars.push(car);
});
var jsonstring = $.toJSON(cars);
$('#CarsList').val(jsonstring);
});
So on my cshtml I have a hidden for of CarsList which is a string on my model. When I set a breakpoint on my method in the controller which the UseCars button calls I can see the value of model.CarsList is a jstrong string but it is finding all the checkboxes in the table and not the particluar ones seletec and it is not pulling the id value of the row out either.
Can anyone see something I am missing here?
EDITED TO INCLUDE CSHTML MARKUP
So this is the markup I have on my page for the table.
<table id="carsTable" style="margin-left: 2px; width: 200px">
<thead>
<tr>
<th class="ui-widget-header">
<input id="SelectAll" type="checkbox" />
</th>
<th class="ui-widget-header table-header">
</th>
<th class="ui-widget-header table-header">
Age of Car (Years)
</th>
<th class="ui-widget-header table-header">
Car Name
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.CarList)
{
<tr>
<td class="table-data">
<input id="SelectIndividual" name="Select" type="checkbox" />
</td>
<td id="InvoiceId">
@Html.DisplayFor(modelItem => item.CarID)
</td>
<td class="table-data">
@Html.DisplayFor(modelItem => item.CarAge)
</td>
<td class="table-data">
@Html.DisplayFor(modelItem => item.CarName)
</td>
</tr>
}
</tbody>
</table>
And then in my js file for the page for the DataTable table config the ID column is set to bvisible: false so it is not actually shown to the user. I changed the id select as Daniel mentioned so it now looks like - car.ID = $(this).next("CarId");
In my breakpoint in the controller I can see info is now returned but not exactly what I was expecting - i.e I was hoping my string would be something like [{123}, {124}] but instead I am getting output like:
"[{\"ID\":{\"length\":0,\"prevObject\":{\"0\":{\"jQuery183036556275907670765\":227},\"context\":{\"jQuery183036556275907670765\":227},\"length\":1},\"context\":{\"jQuery183036556275907670765\":227},\"selector\":\".next(CarId)\"}}]"