如何在ASP.NET Core MVC中使用jquery读取Json数据

8oomwypt  于 2022-11-19  发布在  .NET
关注(0)|答案(1)|浏览(165)

我想在选择下拉列表值时创建表行,但此时它什么也不返回。我尝试更改数据。注意到只有数据,但随后它返回“未定义”。
Returns Empty

检视:

<script>
$(document).ready(function () {
    $('select').formSelect();
});

function onManagerChange() {
    var id = $("#Managers").val();
    var rows = "";

    $.ajax({
        type: "GET",
        url: "@Url.Action("GetFilteredReports", "FinalReports")",
        dataType: "application/json",
        data: { "id": id },
        success: function (data) {
            $("#Reports").html("");
            $.each(data.notices, function (id, report) {
                rows += `
                <tr> +
                <td> + ${report.ReportName} + </td> +
                <td> + ${report.ReportState} + </td> +
                <td> + <a class="btn-small waves-effect waves-light" asp-action="Download" asp-route-id="report.ID">Download</a> + </td> +
                </tr>`
            });
            $("#Reports").html(rows);
        }
    });
}

控制器:

public IActionResult GetFilteredReports(int id)
    {
        var reportsList = _context.Reports.Include(x => x.ReportManager).Where(x => x.ReportManager.ID == id).ToList();

        return Json(reportsList);
    }
pod7payv

pod7payv1#

找到了答案,这就是解决办法

function onManagerChange() {
    $('#Reports tbody').html("");
    var stateName = "";
    var id = $("#Managers").val();
    var downloadURL = '@Url.Action("Download", "FinalReports")';
    $.ajax({
        type: "POST",
        url: "@Url.Action("GetFilteredReports", "FinalReports")",
        dataType: "json",
        data: { id: id },
        success: function (response) {
            $.each(response, function (i, report) {

                if (report.reportState == @((int)ReportState.Pending))
                    stateName = "Pending";
                else if (report.reportState == @((int)ReportState.Accepted))
                    stateName = "Accepted";
                else if (report.reportState == @((int)ReportState.Denied))
                    stateName = "Denied";

                    var row = `
                    <tr>
                        <td> ${report.reportName} </td>
                        <td> ${stateName} </td>
                        <td>
                           <a class="btn-small waves-effect waves-light" href=\"${downloadURL}/${report.id}">Download</a>
                        </td>
                    </tr>`;
                    $('#Reports tbody').append(row);
                });
        }, error: function (data, status, jqXHR) {
            alert('There was an error.');
        }
    });

控制器:

[HttpPost]
    public IActionResult GetFilteredReports(string id)
    {
        var response = _context.Reports.Where(x => x.ReportManager.ID == Int32.Parse(id)).ToList();

        return Json(response);
    }

查看方式:

<form>
    <table id="Reports">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.ReportName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ReportState)
                </th>
                <th>
                    @Html.Raw("File")
                </th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <br />
</form>

相关问题