Asp.net mvc 向导 - 需要一些指导
我需要一些帮助在 asp.net mvc 中创建向导。 该向导将包含大约 7 或 8 个步骤。 这是我对控制器进行建模的方式,我希望得到一些反馈,了解这是否是正确的方法,或者看看这里的人是否可以推荐更好的方法。
- 我为每个步骤创建了一个单独的视图模型类。
- 我有一个 Wizard 类,其中包含每个单独的模型 + CurrentStep 等属性。
- 我为每个步骤创建了一个单独的视图。
我的控制器看起来像这样
public class MyRegistrationController
{
[HttpGet]
public ActionResult Step1()
{
var wizard = TempData[WizardKey] as RegistrationWizard;
RegistrarRegisterVoterNewRegistrant model;
if (wizard == null || wizard.Step1Model == null)
{
wizard = new RegistrationWizard();
model = new NewRegistrant();
}
else
model = wizard.Step1Model;
wizard.CurrentStep = 1;
wizard.Step1Model = model;
TempData[WizardKey] = wizard;
return View("Step1", model);
}
[HttpPost]
public ActionResult Step1(NewRegistrant model)
{
var wizard = TempData[WizardKey] as RegistrationWizard;
if (wizard == null)
wizard = new RegistrationWizard();
if (!ModelState.IsValid)
return View("Step1", model);
wizard.Step1Model = model;
wizard.MaxCompletedStep = 1;
TempData[WizardKey] = wizard;
return RedirectToAction("Step2");
}
[HttpGet]
public ActionResult Step2()
{
var wizard = TempData[WizardKey] as RegistrationWizard;
PersonalInformation model;
if (wizard == null || wizard.Step1Model == null)
return RedirectToAction("Step1");
if (wizard.Step2Model == null)
model = new PersonalInformation ();
else
model = wizard.Step2Model;
wizard.CurrentStep = 2;
TempData[WizardKey] = wizard;
return View("Step2", model);
}
[HttpPost]
public ActionResult Step2(PersonalInformation model)
{
var wizard = TempData[WizardKey] as RegistrationWizard;
if (wizard == null || wizard.CurrentStep != 2 || wizard.Step1Model == null)
return RedirectToAction("Step1");
if (!ModelState.IsValid)
return View("Step2", model);
wizard.Step2Model = model;
wizard.MaxCompletedStep = 2;
TempData[WizardKey] = wizard;
return RedirectToAction("Step3");
}
}
谢谢!
I need some help creating a wizard in asp.net mvc.
This wizard will contain about 7 or 8 steps.
Here's how i've modeled the controller and i was hoping to get some feedback on whether this is the correct approach or to see if there are any better ways that someone here can recommend.
- I've created a separate view model class for each of the steps.
- I have a Wizard class which contains each of these seperate models + Properties like CurrentStep etc.
- I've created a separate view for each of the steps.
My controller looks like this
public class MyRegistrationController
{
[HttpGet]
public ActionResult Step1()
{
var wizard = TempData[WizardKey] as RegistrationWizard;
RegistrarRegisterVoterNewRegistrant model;
if (wizard == null || wizard.Step1Model == null)
{
wizard = new RegistrationWizard();
model = new NewRegistrant();
}
else
model = wizard.Step1Model;
wizard.CurrentStep = 1;
wizard.Step1Model = model;
TempData[WizardKey] = wizard;
return View("Step1", model);
}
[HttpPost]
public ActionResult Step1(NewRegistrant model)
{
var wizard = TempData[WizardKey] as RegistrationWizard;
if (wizard == null)
wizard = new RegistrationWizard();
if (!ModelState.IsValid)
return View("Step1", model);
wizard.Step1Model = model;
wizard.MaxCompletedStep = 1;
TempData[WizardKey] = wizard;
return RedirectToAction("Step2");
}
[HttpGet]
public ActionResult Step2()
{
var wizard = TempData[WizardKey] as RegistrationWizard;
PersonalInformation model;
if (wizard == null || wizard.Step1Model == null)
return RedirectToAction("Step1");
if (wizard.Step2Model == null)
model = new PersonalInformation ();
else
model = wizard.Step2Model;
wizard.CurrentStep = 2;
TempData[WizardKey] = wizard;
return View("Step2", model);
}
[HttpPost]
public ActionResult Step2(PersonalInformation model)
{
var wizard = TempData[WizardKey] as RegistrationWizard;
if (wizard == null || wizard.CurrentStep != 2 || wizard.Step1Model == null)
return RedirectToAction("Step1");
if (!ModelState.IsValid)
return View("Step2", model);
wizard.Step2Model = model;
wizard.MaxCompletedStep = 2;
TempData[WizardKey] = wizard;
return RedirectToAction("Step3");
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的方法似乎是正确的,但步骤操作之间可能有一些代码重复。作为另一种更通用的方法,您可以查看 以下答案。
Your approach seems correct but might have some code repetitions between the steps actions. As an alternative and a little more generic approach you may checkout the following answer.
我并不是将其作为一种更好的方法来出售,因为它完全取决于您的需求,但根据向导的复杂性以及您在每个步骤中保存到数据库的需要,您可以使用类似的方法这个 jquery 表单到向导插件 将您的表单变成向导。它非常简单,可以减少控制器中的代码/复杂性
I'm not selling this as a better approach, because it completely depends on your needs, but depending on the complexity of the wizard and your need to save to the DB on every step you could use something like this jquery Form to Wizard plugin to turn your form into a wizard. It is pretty straightforward and could reduce code/complexity in your controller