我有一个收集大量数据的表单。所有数据都存储在模型对象中。
提交表单后,如果通过验证,模型将传递到确认视图,该视图只显示表单中提交的所有信息。
[HttpPost]
public ActionResult ClientProfile(ClientProfileFormModel model)
{
if (ModelState.IsValid)
{
return View("ClientProfileConfirmation",model);
}
return View(model);
}
当用户点击底部的提交按钮时,我需要模型进入操作,以便我发送电子邮件。
[HttpPost]
public ActionResult ClientProfileConfirmationSubmit(ClientProfileFormModel model)
{
string emailToAdminBody = GenerateFirmProfileAdminEmail(model);
EmailLogic.Instance.SendEmail("test@test.com", "test@test.com", "Firm profile from " + model.FirmAddress.Name, emailToAdminBody);
return View(model);
}
我的问题是:我需要一种简单的方法来从表单的HttpPost操作中获取我的模型(在验证后将其发送到确认页面)到确认的HttpPost操作。我想避免使用隐藏的输入填充确认视图,以便我可以通过表单将其全部写入。
我也尝试将模型存储在Session AND TempData中,但由于某种原因,这两种方式都返回null。我认为这与经历多项行动有关。
这真的不应该那么难!我错过了什么?是将一堆隐藏的输入字段放入确认页面形式的唯一方法吗?
答案 0 :(得分:1)
我使用过TempData,它工作正常。
模特:
public class ClientProfileFormModel
{
public string Name { get; set; }
public string Address { get; set; }
public string Country { get; set; }
}
控制器:
public class ClientController : Controller
{
// GET
public ActionResult ClientProfile()
{
return View();
}
[HttpPost]
public ActionResult ClientProfile(ClientProfileFormModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("ClientProfileConfirmation", model);
}
return View(model);
}
// GET
public ActionResult ClientProfileConfirmation(ClientProfileFormModel model)
{
return View(model);
}
[HttpPost]
public ActionResult ClientProfileConfirmation()
{
var model = (ClientProfileFormModel) TempData["clientProfile"];
string emailToAdminBody = GenerateFirmProfileAdminEmail(model);
EmailLogic.Instance.SendEmail(...);
TempData["success-message"] = "Your profile has been approved. Check your inbox.";
return View("ClientProfileConfirmation", model);
}
}
ClientProfile视图
@model StackOverflow.Models._31465719.ClientProfileFormModel
@{
ViewBag.Title = "ClientProfile";
}
<h2>ClientProfile</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ClientProfileFormModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Country, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
ClientProfile确认视图:
@model StackOverflow.Models._31465719.ClientProfileFormModel
@{
TempData["clientProfile"] = Model;
ViewBag.Title = "ClientProfileConfirmation";
var successMessage = TempData["success-message"];
}
<h2>ClientProfileConfirmation</h2>
@if (successMessage != null)
{
<div class="alert alert-success">
<p class="text-success">@successMessage</p>
</div>
}
@using (Html.BeginForm())
{
<div>
<h4>ClientProfileFormModel</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd>
@Html.DisplayFor(model => model.Address)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Country)
</dt>
<dd>
@Html.DisplayFor(model => model.Country)
</dd>
</dl>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Confirm" class="btn btn-default" />
</div>
</div>
}
我在上一个确认视图中所做的就是将模型保存在TempData中:
TempData["clientProfile"] = Model;
然后在控制器中阅读:
var model = (ClientProfileFormModel) TempData["clientProfile"];