A 有一个简单的网站,有 4 个视图(PersonalInfo、PostOffice、OrderDetails、Commentary)。所有视图都被强类型化为名为 Information 的模型。我需要做的是:
- 当我单击 PersonalInfo 视图中的下一步按钮时,我需要将一些数据存储到会话中
- 之后,我必须被重定向到下一个视图,OrderDetails
下面是我如何尝试解决问题的示例,但它不起作用
控制器:
public ActionResult PersonalInfo(string button, Models.Information FirstName, Models.Information LastName)
{
if (button == "Next")
{
HttpContext.Session["FirstName"] = FirstName;
HttpContext.Session["LastName"] = LastName;
return RedirectToAction("PostOffice", "Home");
}
else if (button == "Back")
{
return RedirectToAction("OrderDetails", "Home");
}
return View();
}
视图中的按钮:
<div>
<button name="button" value="Next">Next</button>
<button name="button" value="Back">Back</button></div>
模型属性:
//Firstname validation
[Required(ErrorMessage = "Required field.")]
[Display(Name = "First name:")]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only.")]
public string FirstName { get; set; }
//Lastname validation
[Required(ErrorMessage = "Required field.")]
[Display(Name = "Last name:")]
[RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only.")]
public string LastName { get; set; }