mardi 24 novembre 2015

Return to redirect, then call JS function

Displaying a Jquery DataTable on screen. Row is selected, new Trial date is selected for that row. Button pressed and necessary row is updated in the controller POST. if (Request.HttpMethod == "POST")All works fine.

I then use a return RedirectToAction("Users", "AziMapOrgs"); to recall the function to update the datatable 'AllOrgsDataTable' which I thought was going to update the jquery dataTable in the view. But it turns out I ALSO need to call function setupPage() in the view to redraw the dataTable.

//controller
 public ActionResult Users(string selectedCheckBoxes, string newDate)
        {
            Models.AziMapOrgsModel orgModel = new Models.AziMapOrgsModel();

            //When Extend Trial button is pressed will be a post back so update users to new Trail End Date
            if (Request.HttpMethod == "POST")
            {
                if (selectedCheckBoxes != "" && newDate != "")
                {
                    orgModel.UpdateUserTrial(selectedCheckBoxes, newDate);
                    return RedirectToAction("Users", "AziMapOrgs");
                }
                //after new exiry date set run through code again to update table with new dates
            }

            //Display all users in dataTable and set ID to 'IDColumn' in jquery dataTable
            ViewModels.AziMapOrgsVM orgViewModel = new ViewModels.AziMapOrgsVM();
            orgViewModel.AllOrgsDataTable = orgModel.GetOrganisations();
            orgViewModel.IDColumn = "ID";

            return View(orgViewModel);
        }

//Users View
    function SetupPage() {
    //draw dataTable
    var table = $('#OrgTable').DataTable();
    if (table.length > 0) {
        table.DataTable({
            "bSort": false,
            "sScrollX": true,
            "bLengthChange": false,
            "scrollY": "75%",
            "scrollCollapse": true,
            "paging": false,
            "oLanguage": {
                "sZeroRecords": "No such record exists"
            },
            "columnDefs": [
            { type: "OrgFreeTrialEnd", targets: 0 }
            ]
        });
    }
    table.draw();

Aucun commentaire:

Enregistrer un commentaire