Asp.net mvc 向导 - 需要一些指导

发布于 2024-12-02 03:41:11 字数 2347 浏览 1 评论 0原文

我需要一些帮助在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韬韬不绝 2024-12-09 03:41:11

您的方法似乎是正确的,但步骤操作之间可能有一些代码重复。作为另一种更通用的方法,您可以查看 以下答案

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.

征﹌骨岁月お 2024-12-09 03:41:11

我并不是将其作为一种更好的方法来出售,因为它完全取决于您的需求,但根据向导的复杂性以及您在每个步骤中保存到数据库的需要,您可以使用类似的方法这个 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文