【问题标题】:MVC Wizard IssuesMVC 向导问题
【发布时间】:2013-03-27 07:29:13
【问题描述】:

我正在尝试在 MVC 中创建一个向导。因为我需要在每一步之后向数据库提交东西,所以我想将数据传回控制器而不是处理这个客户端。不过,我终其一生都无法弄清楚我做错了什么。我有一个包含每个步骤的 ViewModel 的 ViewModel 和一个 StepIndex 来跟踪我在哪里。每个步骤页面都被强类型化到包含的 ViewModel。出于某种原因,当我增加 StepIndex 时,它表明它在控制器中增加了,但它永远不会保留。我有一个隐藏的值,并且传递了 Step1 值。我已经尝试过 model.StepIndex++ 和 model.StepIndex + 1,它们都在控制器中显示为递增,但是在加载视图时使用了不正确的值。我什至关闭了缓存,看看这是否是原因。如果您发现我做错了什么,请告诉我。谢谢你,TJ

包含视图模型

public class WizardVM
{
    public WizardVM()
    {
        Step1 = new Step1VM();
        Step2 = new Step2VM();
        Step3 = new Step3VM();
    }

    public Step1VM Step1 { get; set; }
    public Step2VM Step2 { get; set; }
    public Step3VM Step3 { get; set; }
    public int StepIndex { get; set; }
}

第二步查看

@model WizardTest.ViewModel.WizardVM

@{
    ViewBag.Title = "Step2";
}

<h2>Step2</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    @Html.HiddenFor(model => model.Step1.Foo)
    @Html.HiddenFor(model => model.StepIndex)    
    <fieldset>
        <legend>Step2VM</legend>


        <div class="editor-label">
            @Html.LabelFor(model => model.Step2.Bar)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Step2.Bar)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

控制器

    public ActionResult Index()
    {
        var vm = new WizardVM
            {
                Step1 = { Foo = "test" }, 
                StepIndex = 1
            };

        return View("Step1", vm);
    }

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    [HttpPost]
    public ActionResult Index(WizardVM model)
    {
        switch (model.StepIndex)
        {
            case 1:
                model.StepIndex = model.StepIndex + 1;
                return View("Step2", model);
            case 2:
                model.StepIndex = model.StepIndex + 1;
                return View("Step3", model);
            case 3:
                //Submit here
                break;
        }

        //Error on page
        return View(model);
    }

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 viewmodel wizard


    【解决方案1】:

    在浏览器中检查 Step2 页面,查看隐藏字段的值,确保其值为 2。

    Index(WizardVM) 中放置一个断点以检查是否从Step2 中发布了2 的值。在某些情况下,以前的值将从模型数据中恢复。有时您需要致电ModelState.Clear().Remove("ProeprtyName")

    这将允许您准确缩小问题所在。

    【讨论】:

    • 感谢您的意见。我曾使用 IE 中的开发人员工具来查看该值。在控制器将数据传递到 Step2 视图时,模型显示 StepIndex 应为 2,但隐藏值始终在隐藏值中显示为 1。如何防止恢复以前的值?
    • 其实,我找到了一个相关的答案,这足以说醒了,仔细看看这个。 Here 是另一个让我得到完整答案的问题。
    【解决方案2】:

    感谢 AaronLS 为我指明了正确的方向。上面需要的更改如下。

    在视图页面中将 HiddenFor 更改为 Hidden 像这样 ...

    @Html.Hidden("StepIndex", Model.StepIndex)
    

    并修改控制器以删除每个帖子中的隐藏字段...

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        [HttpPost]
        public ActionResult Index(WizardVM model)
        {
            ModelState.Remove("StepIndex");
    

    感谢Darin Dimitrov 提供解决方案。

    【讨论】:

      猜你喜欢
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      相关资源
      最近更新 更多