簡體   English   中英

將數據從Controller傳遞到View,然后再傳遞回Controller

[英]Passing data from Controller, to View, and back to Controller

目前,我有一種將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() 這可能嗎?

這是您要將模型序列化為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>

您不應該在傳統的MVC應用程序中執行此操作。 您應該在控制器中一次創建並填寫模型,然后單次將其饋送到視圖中,然后將其忘記,直到下一次往返服務器。 您不應該將它來回扔給控制器。

您嘗試做的事情應該通過iframe或客戶端的后續ajax調用來完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM