Typescript调用在一台机器上传递ajax模型,但不在另一台机器上传递

时间:2019-05-30 05:54:43

标签: c# .net typescript asp.net-core

我刚得到一台正在使用的新机器,并且将我的仓库从GIT拉到了新机器上。

在检查并确保一切正常并且建立数据库连接等时,遇到了我没想到的问题。 Typescript在新机器上触发我的事件并进行ajax调用,但未将模型传递给控制器​​。但是,这台旧机器运行良好。

两者上的代码相同,所以我对所发生的事情一无所知。

TypeScript:

prepareDataForSave = () => {

        var trainee = $("#trainee");
        var traineeId = trainee.data("trainee-id");

        var tblData = $('#lesson-report-table > tbody  > tr.competency-row');
        var competenciesArray = new Array();
        var objectivesArray = new Array();
        var flightPhasesArray = new Array();

        var _self = this;

        $.each(tblData, function (trIdx, tblObj: HTMLTableRowElement) {
            var flightPhaseArray = new Array();
            var behavioursArray = new Array();
            var competencyId = $(this).attr('data-competency-id');
            var competencyCode = $(this).closest('tr').find('td.competency-code').text().toString().trim();
            var norm = Number($(this).closest('tr').find('td.norm').text().toString().trim());
            var rowComment = $(this).closest('tr').find('td textarea.lesson-comments').val().toString().trim();
            var gradeNumericTextBoxId = tblObj.id + "-grade";
            var gradeNtb: kendo.ui.NumericTextBox = $("#" + gradeNumericTextBoxId).data("kendoNumericTextBox");
            var grade = gradeNtb.value();
            var cells = tblObj.cells;

            $.each(cells,
                function (idx, cell: HTMLTableCellElement) {
                    //Flight Phase Information
                    if ($(this).attr('data-flight-phase-code') != undefined) {
                        var code = $(this).attr('data-flight-phase-code');
                        var numericTextBoxId = tblObj.id + "-fp-" + code;
                        var numericTextBox: kendo.ui.NumericTextBox = $("#" + numericTextBoxId).data("kendoNumericTextBox");
                        if (numericTextBox.value() !== null) {
                            flightPhaseArray.push({
                                Grade: numericTextBox.value(),
                                ReportLessonFlightPhaseId: $(this).attr('data-flight-phase-id'),
                                ReportLessonCompetencyId: competencyId,
                            });
                        }
                        if (trIdx === 0) {
                            flightPhasesArray.push({
                                FlightPhaseId: $(this).attr('data-flight-phase-id'),
                                ReportLessonId: _self.reportLessonId
                            });
                        }
                    }
                });

            var ul = $(this).closest('tr').next('tr').find('ul');
            $.each($(ul).find('li'),
                function (idx, li: HTMLLIElement) {
                    behavioursArray.push({
                        BehaviourId: $(this).data('behaviour-id'),
                        ReportLessonCompetencyId: competencyId
                    });
                });

            competenciesArray.push({
                Id: competencyId,
                Code: competencyCode,
                Grade: grade,
                Norm: norm,
                Comments: rowComment,
                FlightPhases: flightPhaseArray,
                Behaviours: behavioursArray
            });

        });

        var objectives = $("#objectives").closest('div').find('ul');
        $.each($(objectives).find('li'),
            function () {
                objectivesArray.push({
                    ReportLessonId: _self.reportLessonId,
                    ObjectiveId: $(this).data('objective-id')
                });
            });

        var model = {
            Id: this.reportLessonId == undefined ? 0 : this.reportLessonId,
            EventKey: this.eventKey,
            EventName: this.eventName,
            UserKey: this.userKey,
            UserGuid: this.userGuid,
            TraineeName: this.traineeName,
            InstructorName: this.instructorName,
            Comments: $("#comments").val(),
            Conditions: $("#conditions").val(),
            Date: this.date,
            StartTime: this.startTime,
            EndTime: this.endTime,
            //PlannedDuration:@(Model.Duration),
            Duration: this.duration,
            SignedDate: this.signedDate,
            AcknowledgedDate: this.acknowledgedDate,
            Competencies: competenciesArray,
            Objectives: objectivesArray,
            FlightPhases: flightPhasesArray
        };
        return model;
    }


save = (buttonText: string = "Saving") => {
        $("#bttnSave").html(buttonText + " <i class=\"fas fa-spinner fa-spin \"></i>");
        $("#bttnSave").addClass("btn-warning");
        var model = this.prepareDataForSave();
        var da = new AjaxDataAccessLayer(Tmsp.Enums.AjaxCallType.Post,
            Tmsp.Enums.AjaxDataType.Json,
            "save",
            JSON.stringify(model),
            "application/json; charset=utf-8",
            { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
            true,
            true);

        var _self = this;

        da.ajaxCall(data => {
            _self.reportLessonId = data.id;
            $("#bttnSave").html("Save Report");
            $("#bttnSave").removeClass("btn-warning");
        },
            (xhr, textStatus, errorThrown) => {
                $("#bttnSave").html("Save Report");
                $("#bttnSave").removeClass("btn-warning");
                console.log(xhr);
                console.log(textStatus);
                console.log(errorThrown);
            });

        return false;
    }

C#控制器

[HttpPost]
[Route("save")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SaveReport([FromBody] ReportLesson model)
{
      ... My code here
}

在旧计算机上传递时,模型正确,在新计算机上,模型为空。

如果我将ReportLesson更改为一个对象,我可以看到传入的JSON字符串,但是这样做是不正确的,因为a可以在一台机器上运行,而b则需要将其转换为模型。 / p>

==============编辑===============

我发现问题是Date: this.date,。但是,我仍然不知道为什么在一台计算机上可以运行而另一台计算机却不能运行。

格式为dd / MM / yyyy,当尝试将其作为ISOstring传递时,我遇到类型错误,并且与任何其他日期转换相同。所以我很茫然。

0 个答案:

没有答案