[英]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: 你有一些选择:
TempData
or the session. 您可以将报告数据保存在数据库, TempData
或会话中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.