I have a form which gets it's data using jQueryDataTable.
$('#myTableName').DataTable(
{
"ajax": {
"url": "/API/Loaddata",
"type": "GET",
"datatype": "json"
},
"columns": [
{
"data": "IsSelected",
"render": function (data, type, row) {
if (type === 'display') {
return '<input type="checkbox" class="editor-active">';
}
return data;
},
"className": "dt-body-center"
// "autoWidth": true
},
{ "data": "Name", "autoWidth": true }
],
"rowCallback": function (row, data) {
// Set the checked state of the checkbox in the table
$('input.editor-active', row).prop('checked', data.active == 1);
}
}
);
Now to get data I have following code,
private static IEnumerable<CircState> rCirc;
[AllowAnonymous]
public ActionResult LoadData()
{
rCirc= _repo.GetAll().OrderBy(circ => circ.ID).Select(x => new CState { IsSelected = true, Name = x.Name });
return Json(new { data = circuits }, JsonRequestBehavior.AllowGet);
}
This works fine an I get following JSON, which is bound to my view:
{
"data":
[
{"IsSelected":true,"Name":"SMyDataPoint__01"},
{"IsSelected":true,"Name":"SMyDataPoint__04"},
{"IsSelected":true,"Name":"SMyDataPoint__07"},
{"IsSelected":true,"Name":"SMyDataPoint__08"},
{"IsSelected":true,"Name":"SMyDataPoint__09"},
{"IsSelected":true,"Name":"SMyDataPoint__10"},
{"IsSelected":true,"Name":"SMyDataPoint__11"}
]
}
This is my UI code:
<div class="form-group">
@Html.LabelFor(model => model.Cirs, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-5">
<table id="filtersTable">
<thead>
<tr>
<th>Select</th>
<th>Name</th>
</tr>
</thead>
</table>
</div>
</div>
Now when I am tick/untick an item and click on submit button, How can I get the unslected items.
What I need is two-way binding between View and Model , so When I call following code , I should get all the items based on there state(true/false).
$('#btnRunReport').click(function (e) {
e.preventDefault();
$.ajax({
type: "GET",
url: "/Circuits/SelectedCircs/",
success: function (result) {
console.log(result);
},
error: function (result) {
console.log('Error');
}
});
});
[AllowAnonymous]
public ActionResult SelectedCirs()
{
//rCirc is the global variable decalred on top (In First API)
var selectedCircs = rCirc.Where(x => x.IsSelected == false);
return Json(selectedCircuits);
}
Name, are you wanting to post back just the values of the selected names?IDproperty in the table (if so, you should update the json data andDataTablecode - you will need javascript/jquery to build an array to pass back to the controller based on if the checkbox is checked)? Its also a bit unclear what the method you posting to is going to do