0

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);
        }
4
  • Since the only other property in the json data is Name, are you wanting to post back just the values of the selected names? Commented Jan 22, 2018 at 9:04
  • I have ID also associated with the class but no shown just to keep the code clean. Commented Jan 22, 2018 at 9:22
  • Have you want to get all selected rows with checkbox checked and pass values into action method? This fiddle may show you something until submit request: jsfiddle.net/gyrocode/abhbs4x8. Commented Jan 22, 2018 at 9:26
  • Is there a column for the ID property in the table (if so, you should update the json data and DataTable code - 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 Commented Jan 22, 2018 at 9:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.