Ajax调用是将集合传递给控制器​​而不是字符串数组

时间:2017-06-27 09:47:54

标签: jquery ajax asp.net-mvc

下面给出的代码用于将活动和非活动行程列表传递给控制器​​。



function triptable(tablename, isTripIn, addColumnButton, saveButton, result, tripActiveStatus) {
    this.tableName = tablename;
    this.istripin = isTripIn;
    this.dataTable = null;
    this.rowNum = 1;
    this.colNum = 0;
    this.rowCell = 0;
    this.colCell = 0;
    this.iterator = 0;
    this.colsList = [];
    this.tableData = [{}];
    this.oTable = null;
    this.inEditMode = null;
    this.addColumnId = addColumnButton;
    this.saveId = saveButton;
    this.tripStatus = tripActiveStatus;
    this.MasterScheduleId = [];
    this.init(result);
}

triptable.prototype = {
    init: function (result) {
        //  alert('1');
        this.getSchedules(result);
    },
    getSchedules: function (result) {
        this.colsList = [];
        var returnedData = result;
        if (returnedData.length > 0) {
            var obj = returnedData[0];
            var s = 1;
            for (var key in obj) {
                var attrName = key;
                var trip = key.substr(0, key.indexOf('('));
                var status = _.find(this.tripStatus, function (o) { return o.TripNo == trip });
                var checkBox = "<input type='checkbox' id= 'chk" + s + "' checked></input> Trip" + key;
                if (status != null && status.Status == 0) {
                    checkBox = "<input type='checkbox' id= 'chk" + s + "'></input> Trip" + key;
                    this.MasterScheduleId.push(status.MasterTripId.toString());
                }
                var column = {
                    "mData": key,
                    "mDataProp": key,
                    "sTitle": checkBox,

                    "sType": "string"
                };
                this.colsList.push(column);
                s++;
            }
            results = returnedData;
            var stop = this.colsList[this.colsList.length - 1];
            $('#' + this.tableName + 'ColumnCount').text(this.colsList.length);
            this.colNum = this.colsList.length - 1;
            i = this.colsList.length + 1;
            this.colsList[0].sTitle = "Stop Names";

            this.tableData = returnedData;
            this.setTable();
            this.oTable = this.dataTable;
        }
    },
    setTable: function () {
        //Construct the measurement table
        this.dataTable = $('#' + this.tableName).dataTable({
            "bJQueryUI": true,
            "bDeferRender": true,
            "bInfo": false,
            "bSort": false,
            "bDestroy": true,
            "bFilter": false,
            "bPagination": false,
            "aaData": this.tableData,
            "aoColumns": this.colsList,
            //"scrollY": "220px",
            //"scrollX": false,
            "scrollCollapse": true,
            "paging": false,
            "fixedColumns": {
                "leftColumns": 1,
                "rightColumns": 0
            }
        });

        this.bindTableEvents(this);
    },

    saveRow: function (oTable, nRow) {
        var jqInputs = $('input', nRow);
        oTable.fnUpdate(jqInputs[0].value, this.rowCell, this.colCell, false);
    },

    unbindTableEvents: function (contextData) {
        $('#' + contextData.tableName + ' thead tr th').off('click', contextData.onTableHeaderClicked);
        $('#' + contextData.tableName + ' tbody tr td').off('click', contextData.onTableRowClicked);
        $('#' + contextData.tableName + ' tbody tr').off('keydown', contextData.onTableKeyDown);
        $('#' + contextData.saveId).off('click', contextData.onTableSaveClicked);
        $('#' + contextData.addColumnId).off('click', contextData.onAddColumnClicked);
    },
    bindTableEvents: function (contextData) {
        contextData.unbindTableEvents(contextData);
        $('#' + contextData.tableName + ' thead tr th').on('click', { context: contextData }, contextData.onTableHeaderClicked);
        $('#' + contextData.tableName + ' tbody tr td').on('click', { context: contextData }, contextData.onTableRowClicked);
        $('#' + contextData.tableName + ' tbody tr').on('keydown', { context: contextData }, contextData.onTableKeyDown);
        $('#' + contextData.saveId).on('click', { context: contextData }, contextData.onTableSaveClicked);
        $('#' + contextData.addColumnId).on('click', { context: contextData }, contextData.onAddColumnClicked);
    },

    onTableHeaderClicked: function (ev) {
        var self = ev.data.context;
        var data = ev.currentTarget.innerText;
        data = data.replace("Trip", "");
        data = data.replace(/\n/g, '').trim();
        var MasterTripId = data.substr(0, data.indexOf("("));
        var removed = _.find(self.tripStatus, function (o) { return o.TripNo == MasterTripId });
        var index = _.indexOf(self.MasterScheduleId, removed.MasterTripId.toString());
        if (index >= 0) {
            self.MasterScheduleId = jQuery.grep(self.MasterScheduleId, function (value) {
                return value != removed.MasterTripId.toString();
            });

        }
        else {
            self.MasterScheduleId.push(removed.MasterTripId.toString());
        }
        self.colNum = parseInt($(this).index());
        console.log("column_num =" + self.colNum);
    },
    onTableRowClicked: function (ev) {
        var self = ev.data.context;
        self.colCell = parseInt($(this).index());
        self.rowCell = parseInt($(this).parent().index());
        console.log("Row_num =" + self.rowCell + "  ,  column_num =" + self.colCell);
    },
    restoreRow: function (oTable, nRow) {
        var aData = oTable.fnGetData(nRow);
        var jqTds = $('>td', nRow);
        for (var i = 0, iLen = jqTds.length ; i < iLen ; i++) {
            oTable.fnUpdate(aData[i], nRow, i, false);
        }
    },

    editRow: function (oTable, nRow) {

        var aData = oTable.fnGetData(nRow);
        var jqTds = $('>td', nRow);
        jqTds[this.colCell].innerHTML = '<input type="text" id ="typ" value="' + aData[this.colsList[this.colCell].mData] + '"/>';
    },
    onTableKeyDown: function (ev) {
        var self = ev.data.context;
        if (ev.keyCode == 13) {
            ev.preventDefault();

            if (self.inEditMode == null)
                alert("Select Row");
            else
                self.saveRow(self.oTable, self.inEditMode);
            self.inEditMode = null;
        }
    },
    onAddColumnClicked: function (ev) {

        var self = ev.data.context;
        self.colsList.splice(self.colNum + 1, 0, { "mDataProp": "newField" + self.iterator, sTitle: "Col-" + self.iterator, sType: "string" });
        //update the result, actual data to be displayed
        for (var iRes = 0; iRes < self.tableData.length ; iRes++) {
            self.tableData[iRes]["newField" + self.iterator] = "data-" + self.iterator;
        }
        //destroy the table
        self.dataTable.fnDestroy();
        $('#' + self.tableName + ' thead tr th').eq(self.colNum).after('<th>Trip-' + self.iterator + '</th>');
        //init again
        self.setTable();
        self.iterator++;
        self.bindTableEvents(self);
    },

    onTableSaveClicked: function (ev) {
        debugger;
        var self = ev.data.context;
        var inactiveTrips = self.MasterScheduleId;
        var save = _.reject(self.tripStatus, function (o) { return self.MasterScheduleId.indexOf(o.MasterTripId.toString()) != -1 });
        var masterTrip = _.map(save, 'MasterTripId');

        $.ajax({
            type: "POST",
            url: "/ScheduleManagement/Blocking/SaveBlocking",
            contentType: 'application/json;',
            dataType: "json",
            traditional: true,
            data: JSON.stringify({ inActiveMasterTrip: inactiveTrips, activeMasterTrip: masterTrip }),
            success: function (values) {
            },
            error: function (err) {
                console.log(err);
            }
        });
    }
}
&#13;
 [HttpPost]
        public JsonResult SaveBlocking(List<string> inActiveMasterTrip, List<string> activeMasterTrip)
        {
            int status = _blockingBL.SaveBlockingSheet(activeMasterTrip.ToArray(), inActiveMasterTrip.ToArray());
            return Json(status, JsonRequestBehavior.AllowGet);
        }
&#13;
&#13;
&#13;

onTableSaveClicked:function(ev){是用于保存详细信息的函数。但是我没有得到旅行细节,而是在控制器中低于价值(&#34; Collection&#34;)。

enter image description here  知道为什么价值在控制器中变得如此?

0 个答案:

没有答案