简体   繁体   English

将表单数据传递到确认视图

[英]Passing form data to a confirmation view

I have a simple form that I show to the user with the Create action. 我有一个简单的表单,我通过Create操作向用户显示。 After they have submitted the form, I show them a new Confirmation view, where they can enter their e-mail address to be sent a copy of the form. 提交表单后,我会向他们显示一个新的确认视图,在那里他们可以输入他们的电子邮件地址,然后发送一份表格副本。

In order to fill out the e-mail properly, I need to retain all of the data from the MonthlyReport form that they just completed. 为了正确填写电子邮件,我需要保留刚刚完成的MonthlyReport表单中的所有数据。 However, I am finding it difficult to preserve that report data across Get and Post requests from the Confirmation view. 但是,我发现很难在Confirmation视图中保留Get和Post请求中的报告数据。

When I post from the Confirmation page, I find that my ConfirmationViewModel no longer contains a valid report. 当我从确认页面发布时,我发现我的ConfirmationViewModel不再包含有效的报告。

    [HttpPost]
    public ViewResult Create(MonthlyReport model)
    {
        if (ModelState.IsValid)
        {
            var modelDto = _factory.CreateDataTransferObject(model);
            _repository.Submit(modelDto);

            ModelState.Clear();

            return Confirmation(model);
        }

        return Create();
    }

    [HttpGet]
    public ViewResult Confirmation(MonthlyReport model)
    {
        var confirmation = new ConfirmationViewModel();
        confirmation.Report = model;
        return View("Confirmation", confirmation);
    }

    [HttpPost]
    public ViewResult Confirmation(ConfirmationViewModel model)
    {
        if (ModelState.IsValid)
        {
            try
            {
                var email = _mailer.MonthlyReportEmail(model);
                email.DeliverAsync();
            }
            catch (Exception error)
            {
                Trace.WriteLine(String.Format("Sending mail failed: {0}", error));
            }

            TempData["message"] = "Confirmation e-mail sent.";
        }
        return View("Confirmation");
    }

Here is the Confirmation view: 这是确认视图:

@model Domain.Entities.ConfirmationViewModel

@{
    ViewBag.Title = "Confirmation";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Monthly report for @Model.Report.LearnerName has been saved.</h2>

@using (Html.BeginForm("Confirmation", "MonthlyReport", Model))
{
    <h4>Enter your e-mail address to receive a copy of the report.</h4>
    <table>
        <tr>
            <td colspan="2">@Html.ValidationMessageFor(x => x.EmailAddress)</td>
        </tr>
        <tr>
            <td>@Html.TextBoxFor(x => x.EmailAddress, new { @class = "input-field" })</td>
            <td>
                <input type="submit" class="small-submit-button" value="Send" />
            </td>
        </tr>
    </table>
}

When you display the Confirmation view, you are passing the posted model from the Create action. 显示“ Confirmation视图时,您将从“ Create操作传递已发布的模型。

Once you POST the Confirmation form, you are posting the data in the inputs. 在POST Confirmation表单后,您将在输入中发布数据。 Since you don't have inputs for the Report's properties... this information is not posted. 由于您没有报告属性的输入...此信息不会发布。

You have some options: 你有一些选择:

  1. Add hiddens inputs for each relevant property in the Report model 在报告模型中为每个相关属性添加hiddens输入
  2. Save the information in the server, and retrieve it once the user post the Confirmation form. 将信息保存在服务器中,并在用户发布确认表单后检索。 You can save the Report data in the database, TempData or the session. 您可以将报告数据保存在数据库, TempData或会话中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 IEnumerable 传递给视图以生成表单,然后将表单数据传回 controller - Passing an IEnumerable to a view to generate a form and then passing form data back to controller ASP MVC将视图中的模型数据作为表单数据传递回 - ASP MVC passing model data in view back as form data 将数据从视图传递到控制器以及表单的值 - Passing data from a View to a Controller alongside a Form's values ListView对象,并将数据传递给Xamarin表单中的下一个视图 - ListView Object and Passing data to the next View in Xamarin Form MVC查看数据传递 - MVC view data passing MVC数据在视图中传递 - MVC data passing in view 在同一视图中将数据从一种形式传递到另一种形式的操作方法 - Passing data from one form to the action method of another form within the same view 当DataGridView中有更改的数据时,如何要求关闭表单进行确认 - How to Require Confirmation on Form Close When there is Changed Data in a DataGridView 在 ASP.NET MVC 中将 HTML 表单数据从视图传递到控制器 - Passing HTML form data from View to Controller in ASP.NET MVC 将文本框的值从View传递到Controller中的方法/操作,然后将数据从响应传递回表单 - Passing a value of a textbox from View to a method/action in Controller and then data from response back to form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM