A有一个简单的网站,有4个视图(PersonalInfo,PostOffice,OrderDetails,Commentary)。所有视图都强烈地键入名为Information的模型。我需要做的是:
下面是我尝试解决问题的示例,但它不起作用
控制器:
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; }
答案 0 :(得分:1)
听起来您甚至没有产生要求进入PersonalInfo
行动的请求。
改为按钮提交按钮
<input type="submit" name="button" value="Prev" />
<input type="submit" name="button" value="Next" />
我假设您的表单操作是PersonalInfo
在您的控制器操作中,使用TempData
代替Session
将信息传递给重定向的操作,以便数据仅保留到下一个请求。
TempData["FirstName"] = FirstName;
TempData["LastName"] = LastName;
最后,multi-step registration process issues in asp.net mvc (splitted viewmodels, single model)可能会帮助您,因为您正在进行多步表单处理。