我习惯于使用Web表单,但现在转换到MVC 5,并且有一个创建多步应用程序表单的问题。
这个表单就像向导一样,在最后显示输入的每个步骤中的信息,然后提交。
使用HTML表单在.cshtml中编写这个表单是否更容易?还是全部在控制器中完成?
谢谢!
我习惯于使用Web表单,但现在转换到MVC 5,并且有一个创建多步应用程序表单的问题。
这个表单就像向导一样,在最后显示输入的每个步骤中的信息,然后提交。
使用HTML表单在.cshtml中编写这个表单是否更容易?还是全部在控制器中完成?
谢谢!
[HttpPost]
public ActionResult Step1(Step1ViewModel model)
{
if (ModelState.IsValid)
{
Session["Step1"] = model;
return RedirectToAction("Step2");
}
// errors
return View(model);
}
然后,你的最终POST操作:
[HttpPost]
public ActionResult StepFinal(StepFinalViewModel)
{
if (ModelState.IsValid)
{
var myEntity = new MyEntity();
var step1 = Session['Step1'] as Step1ViewModel;
myEntity.SomeField = step1.SomeField;
// ... repeat for field in view model, then for each step
db.MyEntities.Add(myEntity);
db.SaveChanges();
Session.Remove('Step1');
// repeat for each step in session
return RedirectToAction("Success");
}
// errors
return View(model);
}
@using (Html.BeginForm("Controller Action Method", "Controller Name", FormMethod.Post, new { id = "Form Name" }))
{
// Form Elements here
}
然后您可以简单地添加一个提交按钮,将表单提交到您的控制器进行处理。