ASP.NET MVC向导-需要一些指导 [英] Asp.net mvc wizard - Need some guidance

查看:66
本文介绍了ASP.NET MVC向导-需要一些指导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,在asp.net mvc中创建向导. 该向导将包含大约7或8个步骤. 这是我对控制器建模的方式,我希望能获得一些反馈,以了解这是否是正确的方法,或者希望看看这里是否有人可以推荐更好的方法.

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.

  • 我为每个步骤创建了一个单独的视图模型类.
  • 我有一个Wizard类,其中包含这些单独的模型中的每个模型以及诸如CurrentStep等的属性.
  • 我为每个步骤创建了一个单独的视图.

我的控制器看起来像这样

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");
    }
}

谢谢!

推荐答案

您的方法似乎是正确的,但在步骤操作之间可能会有一些代码重复.作为一种替代方法,您可以通过

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.

这篇关于ASP.NET MVC向导-需要一些指导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆