我有一个接受大量数据的表单。所有数据都存储在一个模型对象中。
当提交表单时,如果通过验证,模型将传递给确认视图,该视图仅显示表单中提交的所有信息。
[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和TempData中,但由于某些原因,这两种方法都返回null。我认为这与经历多个动作有关。
这真的不应该那么难!我遗漏了什么?将一堆隐藏的输入字段放入确认页的表单中是唯一的方法吗?
发布于 2015-07-17 01:05:34
我用过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"];
https://stackoverflow.com/questions/31465719
复制相似问题