简体   繁体   中英

Passing data from Controller, to View, and back to Controller

Currently I have a method that is loading a PDF into an HTML page. The PDF is dynamically created after retrieving data from the database. Now, what I would like to do, is before the view of the PDF is loaded, I'd like to show a page that lets the user know that the information is being loaded.

ActionParams:

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

Controller:

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>

So, from the PreLoaderView, I'd like to pass the @Model back to the controller for use with the SolicitorActionReport_Load() . Is this possible to do?

This is what you want to serialize the model to Javascript so it can be passed to the ajax call:

<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>

You are not supposed to do that in a classical MVC app. You are supposed to create and fill out the model once in the controller then feed it to the view in a single pass and after that forget about it till the next round trip to the server. You should not toss it back and forth to the controller.

What you are trying to do is supposed to be done by either an iframe or a subsequent ajax call from the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM