2014-02-28 260 views
0

目前我有一種將PDF加載到HTML頁面的方法。 PDF是從數據庫中檢索數據後動態創建的。現在,我想要做的是在加載PDF視圖之前,我想顯示一個讓用戶知道信息正在加載的頁面。將數據從控制器傳遞到視圖並返回到控制器

ActionParams:

@using (Html.BeginForm("PreLoaderView", "Report", FormMethod.Post, new { 
    @target = "_blank" 
})) 

控制器:

public ActionResult PreLoaderView(TestViewModel viewModel) { 
    return View(viewModel); 
} 

public ActionResult SolicitorActionReport_Load(TestViewModel viewModel) { 
    var cultivationModel = new CultivationModel(viewModel, ConstituentRepository, CampaignRepository); 
    var cultivationData = cultivationModel.GetCultivationActivityData(); 
    var reportParamModel = new List<ReportParamModel> 
            { 
             new ReportParamModel() {AgencyName = SelectedUserAgency.AgencyName, StartDate = viewModel.StartDate, EndDate = viewModel.EndDate} 
            }; 
    return FileContentPdf("Constituent", "CultivationActivityManagement", cultivationData, reportParamModel, new List<FundraisingAppealMassSummary>(), new List<FundraisingAppealPortfolioSummary>()); 
    return Content(viewModel.SolicitorType); 
} 

PreLoaderView:

@model Report.SolicitorActionParamsViewModel 

@{ 
    ViewBag.Title = "Cultivation Activity"; 
} 
<script type="text/javascript"> 
    $(function() { 
     $.ajax({ 
      url: '@Url.Action("SolicitorActionReport_Load", "Report", @Model)', 

      complete: function() { 
       $('#progress').hide(); 
      }, 
      error: function() { 
       alert('oops'); 
      } 
    }); 
    }); 
</script> 

<div align="center"> 
    <img id="progress" src="~/Content/images/loading.GIF"> 
    <div id="result"> 
     <embed width="100%" height="800px" src="http://localhost/YP/Report/SolicitorActionReport_Load"type="application/pdf"> 
    </div> 
</div> 

所以,從PreLoaderView,我想通過@Model回控制器與SolicitorActionReport_Load()一起使用。這可能嗎?

回答

0

這是要序列模型爲Javascript什麼,因此它可以被傳遞給Ajax調用:

<script type="text/javascript"> 
    $(function() { 
     $.ajax({ 
      url: '@Url.Action("SolicitorActionReport_Load", "Report", 
         @Html.Raw(new JavaScriptSerializer().Serialize(Model)))', 

      complete: function() { 
       $('#progress').hide(); 
      }, 
      error: function() { 
       alert('oops'); 
      } 
     }); 
    }); 
</script> 
0

你不應該做的是,在一個經典的MVC應用程序。您應該在控制器中創建並填寫模型,然後將其一次饋送到視圖中,然後將其忘記,直到下一次到服務器的往返。你不應該把它拋在控制器上。

你要做的事情應該是由客戶端的iframe或後續的ajax調用完成。

相關問題