2017-03-14 27 views
0

我使用Ajax在我的Web应用程序中填充部分视图。此部分视图包含一个链接,用于根据当前位于模型的表/列表<>中的数据下载PDF文件。ASP.NET MVC - Html.Action发送空列表

局部视图如下:

@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被调用时,摘要列表是空的。该列表不为空,但仅包含没有项目。

但是,我不确定为什么会出现这种情况。当我点击导出链接时,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); 
      }, 

     }); 
    } 
}); 

感谢任何帮助。

+0

您无法使用'new {summary = @ Model.reportsList}'将GET复杂对象的集合发送到GET方法' - 查看它生成的要理解的url。但是,发送回刚刚发送给服务器的整个模型是什么意思?(这只会降低性能)。只需发回用于生成视图的4个参数,以便重新构建您需要创建PDF的'List '。 –

回答

0

你问了一点Get方法 - 你应该改用一个表单来允许模型绑定来处理复杂的列表对象。