如果您的要求是构建某种向导,则需要一种在步骤之间维护状态的方法。
ViewBag 对此没有好处,因为您应该在每个向导步骤中遵循 PRG(发布/重定向/获取)模式。
TempData 可用于在步骤之间向前导航,但如果用户返回或直接导航到某个步骤,则会失败。
因此,您需要寿命更长的东西。ASP.NET Session 对象或数据库都是很好的候选对象。
这是一个例子:
public class WizardController : Controller
{
public ActionResult Step1()
{
var session = GetWizardSession();
if (session.Step1 == null)
{
session.Step1 = new Step1View
{
PricingSecurityIds = new SelectList(new[] { 1, 2, 3, 4, 5 }),
SomeOtherIds = new SelectList(new[] { 1, 2, 3, 4, 5 })
};
}
return View(session.Step1);
}
[HttpPost]
public ActionResult Step1(Step1View cmd)
{
var session = GetWizardSession();
// save the wizard state
session.Step1.SelectedPricingSecurityId = cmd.SelectedPricingSecurityId;
session.Step1.SelectedSomeOtherId = cmd.SelectedSomeOtherId;
// now onto step 2
session.Step2 = new Step2View
{
PricingSecurityId = cmd.SelectedPricingSecurityId,
SomeOtherId = cmd.SelectedSomeOtherId,
Name = "John Smith"
};
return RedirectToAction("step2");
}
public ActionResult Step2()
{
return View(GetWizardSession().Step2);
}
public WizardSession GetWizardSession()
{
var session = Session["wizardsession"];
if (session == null)
{
session = new WizardSession();
Session["wizardsession"] = session;
}
return session as WizardSession;
}
}
public class Step1View
{
public SelectList PricingSecurityIds { get; set; }
public SelectList SomeOtherIds { get; set; }
public int SelectedPricingSecurityId { get; set; }
public int SelectedSomeOtherId { get; set; }
}
public class Step2View
{
public int PricingSecurityId { get; set; }
public int SomeOtherId { get; set; }
public string Name { get; set; }
}
public class WizardSession
{
public Step1View Step1 { get; set; }
public Step2View Step2 { get; set; }
}
- 在第 1步中,我们调用
GetWizardSession
. 这会从 ASP.NET 返回一个对象,Session
其中包含我们为向导中的每个步骤收集的所有信息。在这个例子中,我们只是为每个步骤(即session.Step1
)存储 ViewModel。
- 我们检查会话中是否存在 Step1,如果不存在则创建它。然后我们将 Step1 模型传递给我们的视图。
- 当用户提交表单时,我们会更新
session.Step1
. 这确保如果用户导航回 /step1,我们“记住”他们的值。然后我们为Step2构建模型并将其保存在会话中。
- 当我们导航到 /step2 时,我们假设会话中存在模型(因为它们应该从 step1 到这里)所以我们只返回
return View(GetWizardSession().Step2);
观点:
第1步
@model MvcWizardDemo.Controllers.Step1View
@{
ViewBag.Title = "Step1";
}
<h2>Step1</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Step1View</legend>
<div class="editor-label">
@Html.LabelFor(m => m.PricingSecurityIds)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.SelectedPricingSecurityId, Model.PricingSecurityIds)
@Html.ValidationMessageFor(m => m.PricingSecurityIds)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.SomeOtherIds)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.SelectedSomeOtherId, Model.SomeOtherIds)
@Html.ValidationMessageFor(m => m.SomeOtherIds)
</div>
<p>
<input type="submit" value="Next" />
</p>
</fieldset>
}
第2步
@model MvcWizardDemo.Controllers.Step2View
@{
ViewBag.Title = "Step2";
}
<h2>Step2</h2>
Hi, @Model.Name you selected the following values in the previous step:
<p>
<strong>Security Id:</strong> @Model.PricingSecurityId
</p>
<p>
<strong>Some other Id:</strong> @Model.SomeOtherId
</p>