这是我的解决方案。这是一个工作示例,它将在步骤之间遍历相同的用户模型。它使用模型绑定概念。需要对代码进行更多改进,包括适当的验证、数据时间处理等。在最后一步结束时,您应该会看到完成的填充模型,您可以将其保存。
型号 -
public class User
{
public string Fname { get; set; }
public string Lname { get; set; }
public List<AvailabilityDates> Dates { get; set; }
}
public class AvailabilityDates
{
public DateTime? date { get; set; }
}
控制器动作 -
public class UserController : Controller
{
public ActionResult Index()
{
User u = new User();
return View(u);
}
public ActionResult FirstStep(User u)
{
u.Dates = new List<AvailabilityDates>();
u.Dates.Add(new AvailabilityDates() { date = null });
u.Dates.Add(new AvailabilityDates() { date = null });
return View(u);
}
public ActionResult LastStep(User u)
{
// Do your stuff here
return null;
}
}
索引视图 -
@model MVC.Controllers.User
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("FirstStep", "User", FormMethod.Post))
{
@Html.LabelFor(model => model.Fname, new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Fname)
@Html.ValidationMessageFor(model => model.Fname)
@Html.LabelFor(model => model.Lname, new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Lname)
@Html.ValidationMessageFor(model => model.Lname)
<input type="submit" value="Create" class="btn btn-default" />
}
第一步视图 -
@model MVC.Controllers.User
@{
ViewBag.Title = "FirstStep";
}
<h2>FirstStep</h2>
@using (Html.BeginForm("LastStep", "User", FormMethod.Post))
{
@Html.HiddenFor(model => model.Fname);
@Html.HiddenFor(model => model.Lname);
for (int i = 0; i < Model.Dates.Count; i++)
{
@Html.LabelFor(model => Model.Dates[i].date, new { @class = "control-label col-md-2" })
@Html.EditorFor(model => Model.Dates[i].date)
@Html.ValidationMessageFor(model => Model.Dates[i].date)
}
<input type="submit" value="Create" class="btn btn-default" />
}
输出 -
我还建议您研究一些 JQuery 好的向导控件 -