ASP.NET MVC - Html.Action发送空列表

时间:2017-03-14 15:21:21

标签: c# ajax asp.net-mvc

我正在使用Ajax在我的Web应用程序中填充局部视图。该局部视图包含基于当前在表/ List<>中的数据下载PDF文件的链接。该模型。

部分视图如下:

    @model Inspection_Reports.ViewModel.SummaryReportViewModel

<table id="summaryReportTable" class="table-condensed table-striped">
    <thead><tr><td>Inspector</td><td>Attendant</td><td>Property</td><td>Room Number</td><td>Date</td><td>HK Score</td><td>Maint. Score</td></tr></thead>
    <tbody id="resultsContainer">

        @foreach (var report in @Model.reportsList)
        {

            <tr><td>@report.inspect.empName</td><td>@report.attendant.empName</td><td>@report.location.locName</td><td>@report.room</td><td>@report.endTime</td><td>@report.hkDisplay</td><td>@report.mainDisplay <input type='hidden' name='reportId[i]' /></td></tr>


        }

    </tbody>
</table>


@Html.ActionLink("Export as PDF", "GenerateSummaryPDF",  new { summary = @Model.reportsList  })

GenerateSummaryPDF方法:

      public FileResult GenerateSummaryPDF(List<report_summary> summary)      {


        Document doc = pdfWorker.readyDocument("Inspection Report, Generated " + DateTime.Now.ToString("MM-dd-yyyy"));
        pdfWorker.createSummaryReport(doc, summary);
       pdfWorker.savePDF(doc, String.Format("{0}/Inspection_Summary_{1}.pdf", @"C:\Users\Khandokar\Desktop", DateTime.Now.ToString("MM-dd-yyyy")));
       return File(String.Format("{0}/Inspection_Summary_{1}.pdf", @"PATH", DateTime.Now.ToString("MM-dd-yyyy")), "application/pdf", "Inspection.pdf");

问题在于,当调用GenerateSummaryPDF时,摘要列表为空。该列表不为null,但仅包含其中的项目。

但是,我不确定为什么会这样。当我单击导出链接时,Model.reportsList中有数据;它在表格中可见,并通过设置断点进一步验证。

父视图:

    @model Inspection_Reports.ViewModel.SummaryReportViewModel
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<!DOCTYPE html>

<html>
<head>

    <meta name="viewport" content="width=device-width" />
    <title>Report Summaries</title>
</head>
<body>
    <h2>Summary Reports</h2>
    <form class="form-horizontal">
        <div class="form-group"><label class="control-label col-md-2">Start Date: </label><div class="col-md-4"><input class="form-control summaryFilter" type='text' value="@Model.fromDate" name='startDate' id='startDate' /></div><label class="control-label col-md-2">End Date: </label><div class="col-md-4"><input type='text' value="@Model.toDate" class='form-control summaryFilter' name='endDate' id='endDate' /></div></div>

        <div class="form-group">
            <label class="control-label col-md-2">Filter By: </label>
            <div class="col-md-4">
                <select class="form-control summaryFilter" name="filterTypeList" id="filterTypeList">
                    <option value="">Select...</option>
                    <option value="Property">Property</option>
                    <option value="Attendant">Attendant</option>
                    <option value="Inspector">Inspector</option>
                </select>
            </div>
            <label class="control-label col-md-2">Filter Selection: </label><div class="col-md-4">
                <select class="form-control summaryFilter" name="filterSelectionList" id="filterSelectionList"></select>
            </div>
        </div>

    </form>
    <div id="reportResults">
        @{Html.RenderPartial("SummaryPartialView", Model);}
    </div>
    @section scripts {
        <script src="~/Scripts/ajaxReports.js"></script>
    }
</body>
</html>

用于填充局部视图的方法(主要基于本文:https://cmatskas.com/update-an-mvc-partial-view-with-ajax/

[HttpGet]
    public async Task<ActionResult> GetSummaryReports(string fromDate, string toDate, string filterType, string filterValue)
    {
        DateTime from = Convert.ToDateTime(fromDate);
        DateTime to = Convert.ToDateTime(toDate);
        Int32 filterValID = Int32.Parse(filterValue);

        SummaryReportViewModel vm = await GetSummaryVM(from, to, filterType, filterValID);
        return PartialView("SummaryPartialView", vm);
    }


    private async Task<SummaryReportViewModel> GetSummaryVM(DateTime from, DateTime to, string filterType, int filterValID)
    {
        SummaryReportViewModel vm = new SummaryReportViewModel();
        to = to.AddDays(1);

        var reports = dbContext.report_summary.Where(r => r.endTime <= to && r.endTime >= from);
        if (filterType.Equals("Property"))
        {
          reports =  reports.Where(r => r.locationID == filterValID);
        }
        else if (filterType.Equals("Attendant"))
        {
            reports = reports.Where(r => r.employee == filterValID);
        }
        else
        {
            reports = reports.Where(r => r.inspector == filterValID);
        }

        vm.reportsList = reports.ToList<report_summary>();
        return vm;
    }

Ajax

    $(".summaryFilter").change(function () {
    var fromDate = $("#startDate").val();
    var toDate = $("#endDate").val();


    var filterType = $("#filterTypeList").val();
    var filterValue = $("#filterSelectionList").val();

    if (filterValue != null || typeof (filterValue) != typeof (undefined)) {

        $.ajax({
            url: "GetSummaryReports?fromDate=" + fromDate + "&toDate=" + toDate + "&filterType=" + filterType + "&filterValue=" + filterValue,
            type: 'get',
            success: function (data) {
                $("#reportResults").html(data);
            },

        });
    }
});

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你要问一点Get方法 - 你应该转而使用Posts的形式来允许模型绑定来处理复杂的列表对象。