ASP.NET MVC控制器返回JSON List <t>未知错误

时间:2019-05-26 22:55:12

标签: c# jquery json ajax asp.net-mvc

我只是想将List集合从控制器作为JSON字符串发送回ajax函数,并对其进行接收,以便我可以操纵视图。

我之前已经使用集合完成了此操作,但是我看不出问题是什么,我根据底部的图像链接获取了所需的json。但是,调试时,除了浏览器控制台的其他链接中的“ json响应-未定义”之外,我没有看到任何错误。

我已经尝试过: 1.创建一个新的SalaryCalculator数据对象(可以回传) 2.还创建了一个匿名类型(再次发回正常)

控制器方法

    [HttpPost]
    public ActionResult GetSalaryCalculation(List<SalaryCalculator> form)
    {
        foreach (var entry in form)
        {
            entry.Tax = TaxCalculation(entry.Salary);
            entry.MonthlyNet = MonthlyCalculation(entry.Salary, entry.Tax);
            entry.WeeklyNet = WeeklyCalculation(entry.MonthlyNet);
            entry.HourlyRate = HourlyCalculation(entry.WeeklyNet, entry.WeeklyHours);

            if (entry.OverTimeHours > 0)
            {
                entry.OvertimeTotal = OvertimeCalculation(entry.OverTimeHours, entry.HourlyRate);
            }

            entry.OvertimeSalaryTotal = TotalCombinedCalculation(entry.MonthlyNet, entry.OvertimeTotal);
            entry.TaxCode = "tax";

            if (entry.Pension > 0)
            {
                entry.Pension = PensionCalculation(entry.OvertimeSalaryTotal, entry.Pension);
            }

            if (entry.StudentLoan > 0)
            {
                entry.StudentLoan = StudentLoanCalculation(entry.OvertimeSalaryTotal, entry.StudentLoan);
            }
        }

        return Json(form, JsonRequestBehavior.AllowGet);
        //return Content(JsonConvert.SerializeObject(form));
    }

AJAX通话

initialise: function () {

    $("#calculateAmount").on("click", function () {

        var formData = [
            {
                Salary: $("#salaryAmount").val(),
                WeeklyHours: $("#hoursWorked").val(),
                StudentLoan: $("#studentValidation").val(),
                Pension: $("#pensionValidation").val(),
                OverTimeHours: $("#overtimeValidation").val()
            }
        ];

        //console.log(formDataArray);

        Ajax.fn.ajaxPost("GetSalaryCalculation",
            function (jsonSuccess) {

                console.log(jsonSuccess);
            },
            function (xhr, status, error) {

                console.log(xhr);
                console.log(status);
                console.log(error);
            },
            { form: formData }
        );
    });
}

处理AJAX

            $.ajax({
            type: "POST",
            url: "/Home/" + sFunction,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            data: data === null ? null : JSON.stringify(data),
            success: function (response, status, jqXhr) {

                if (typeof response.d !== "undefined") {
                    onSuccess(response.d, status, jqXhr, passThroughData);

                } else {
                    onSuccess(response, status, jqXhr, passThroughData);
                }
            },
            error: function (jqXhr, status, errorName) {
                // Handle generic errors if they exist, otherwise forward to error handler

                if (jqXhr.status === 401) {
                    // Unauthorised. Force a refresh
                    window.location.href = window.location.href;
                    return;
                }
                else if (status === "timeout") {
                    // Function call timeout

                }
                onError(jqXhr, status, errorName, passThroughData);
            },
            timeout: iTimeoutMillis,
        });

索引视图

<form id="calculateForm">
                <div class="form-group row">
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="salaryAmount" placeholder="Salary amount £" aria-label="Salary Amount" aria-describedby="salary Amount" required>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="hoursWorked" placeholder="Weekly hours worked" aria-label="Hours Worked" aria-describedby="Hours Worked" required>
                    </div>
                </div>
                <div class="form-group row collapse" id="studentLoan">
                    <div class="col-sm-10">
                        <input type="text" class="form-control" placeholder="Student loan £" aria-label="Student Loan" id="studentValidation" aria-describedby="Student Loan">
                    </div>
                </div>
                <div class="form-group row collapse" id="pensionPayment">
                    <div class="col-sm-10">
                        <input type="text" class="form-control" placeholder="Pension Payment £" aria-label="Pension Payment" id="pensionValidation" aria-describedby="Pension Payment">
                    </div>
                </div>
                <div class="form-group row collapse" id="overtimeAdjustment">
                    <div class="col-sm-10">
                        <input type="text" class="form-control" placeholder="Overtime hours" aria-label="Overtime Amount" id="overtimeValidation" aria-describedby="Overtime Amount">
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-10">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" data-toggle="collapse" href="#studentLoan" id="studentCheck">
                            <label class="form-check-label" for="studentLoan">
                                Student loan repayment
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-10">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" data-toggle="collapse" href="#pensionPayment" id="pensionCheck">
                            <label class="form-check-label" for="pensionPayment">
                                Pension payment
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-10">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" data-toggle="collapse" href="#overtimeAdjustment" id="overtimeCheck">
                            <label class="form-check-label" for="overtimeAdjustment">
                                Overtime hours
                            </label>
                        </div>
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-10">
                        <button type="submit" class="btn btn-outline-primary" id="calculateAmount">Calculate</button>
                    </div>
                </div>
            </form>

浏览器控制台显示:

browser console display

显示json的控制器动作:

controller action displaying json

controller debugging with form data recieved from ajax

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,这似乎与JavaScript处理十进制值的方式(IE本身不是这种方式)以及从输入表单输入数据的方式有关。

使用显式转换Javascript中的用户输入

parseFloat()。toFixed()

然后通过AJAX调用将数据传递给控制器​​似乎可以解决此问题,但我将研究寻找一种以另一种方式计算数据的方法。