将数据从Controller传递到View,然后传回Controller

时间:2022-10-18 09:33:21

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.

目前我有一个方法将PDF加载到HTML页面。从数据库中检索数据后动态创建PDF。现在,我想要做的是,在加载PDF的视图之前,我想显示一个页面,让用户知道正在加载信息。

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?

因此,从PreLoaderView,我想将@Model传递回控制器以与SolicitorActionReport_Load()一起使用。这可能吗?

2 个解决方案

#1


0  

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

这就是你想要将模型序列化为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>

#2


0  

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.

你不应该在经典的MVC应用程序中这样做。您应该在控制器中创建并填充模型一次,然后在一次通过中将其提供给视图,然后忘记它直到下一次往返服务器。你不应该来回控制器。

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

您要做的事情应该是通过iframe或来自客户端的后续ajax调用来完成的。

#1


0  

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

这就是你想要将模型序列化为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>

#2


0  

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.

你不应该在经典的MVC应用程序中这样做。您应该在控制器中创建并填充模型一次,然后在一次通过中将其提供给视图,然后忘记它直到下一次往返服务器。你不应该来回控制器。

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

您要做的事情应该是通过iframe或来自客户端的后续ajax调用来完成的。