MVC控制器返回不同的视图;模型数据未保存在隐藏输入中 [英] MVC Controller returning different views; Model data not being saved in hidden input

查看:77
本文介绍了MVC控制器返回不同的视图;模型数据未保存在隐藏输入中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用单个控制器动作的多页表单,并根据模型上的值返回视图.

I'm attempting a multiple page form where I use a single controller action and returning a view depending on a value on my model.

我的模型具有使用Html.HiddenFor()在视图中的输入字段中放置的属性.

My model has a property that I put in an input field on my views, using Html.HiddenFor().

这是我的简化模型:

public class MyModel 
{
   public virtual int Step { get; set; }
}

在我看来,我有:

@model MyModel
...
@Html.HiddenFor(model => model.Step)

然后在我的控制器中有:

Then in my controller I have:

public ActionResult Create()
{
    ...
    myModel.Step = 1;
    return View("View1", myModel);
}

[HttpPost]
public ActionResult Create(MyModel myModel)
{
    ...
    if (myModel.Step == 1)
    {
        myModel.Step = 2;
        return View("View2", myModel);
    }
    else if (myModel.Step == 2)
    {
        ...
    }
    ...
}

我的问题是,我的控制器总是看到mymodel.Step的值为1.为什么?

My problem is, my controller always sees mymodel.Step as having the value of 1. Why is that?

奇怪的是,我尝试使用以下命令在表单上显示它:

What's weird is that I tried to display it on the form with these:

@Html.DisplayFor(model => model.Step)
@Html.EditorFor(model => model.Step)

第二次显示页面时,第一行显示文本"2".第二个显示的输入字段为"1".我很困惑.

The second time the page was displayed, the first line showed the text "2". The second showed an input field with "1". I'm confused.

其他信息:

我的模型还具有一个Guid属性,该属性在隐藏字段中传递到视图"上.我也尝试过在回发时更改它,并第二次检查它的值.新值未注册.该模型在第一篇文章之前返回了原始值.因此,它与另一个字段是一致的.

My model also has a Guid property which is passed onto the View in a hidden field. I tried to change it also on postback, and check its value the second time around. The new value did not register. The model returned the original value before the first post. So it is consistent with the other field.

如果我无法找到为什么它现在表现的方式,我可能不得不使用不同的控制器动作.

I may have to use different controller actions if I couldn't find why it is behaving the way it does at the moment.

解决方案:

如下面Reda所建议的,我通过在后操作方法中进行了修复:

As Reda suggested below, I fixed it by doing this in my post action method:

  • 在显示"View2"并更改控制器对模型中的值进行更改之前,我运行ModelState.Clear()

这是博客文章确认需要清除此方案的ModelState.

Here is a blog post which confirms the need to clear ModelState for this scenario.

推荐答案

通常,当您从发布操作返回查看时,这意味着在验证过程中失败了,应该再次使用提交的值来显示表单.这就是为什么ModelState会在您返回View时记住您的输入,并且您的输入将由ModelState而不是您的视图模型填充的原因.

Usually, when you return to view from your post action, it means that something failed during validation process and the form should be displayed again with the submitted values. That's why the ModelState remembers your inputs when you return to View, and your inputs will be filled from the ModelState, not your view model.

我认为您有两种解决方案:

In my opinion you have two solutions :

  • ModelState.Clear,它将在设置新值之前擦除您的旧值
  • 使用新值重定向到GET操作
  • ModelState.Clear, which will erase your old value, before setting new ones
  • redirecting to a GET action, with new values

第二种解决方案是一种更好的解决方案,因为您不会显示带有验证错误的旧表格,而只是显示具有不同值的新视图.

Second solution is a better one, because you're not displaying the old form with validation errors, you're just showing a new view with different values.

这是一个例子(当然,您可以根据需要进行调整):

Here's an example (of course you adapt it to your needs) :

public ActionResult Create(int? step)
{
    ...
    myModel.Step = step.HasValue ? step.Value : 1; // or whatever logic you need to apply
    return View("View1", myModel);
}

[HttpPost]
public ActionResult Create(MyModel myModel)
{
    ...
    if (myModel.Step == 1)
    {
        return RedirectToAction("Create", new { step = 2 });
    }
    else if (myModel.Step == 2)
    {
        ...
    }
    ...
}

这篇关于MVC控制器返回不同的视图;模型数据未保存在隐藏输入中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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