3

我有一个特殊的问题 - 我有一个带有列表的 ViewModel,用于显示图像列表:

public List<int> TrackerKeys { get; set; }

这在页面的两个地方使用:

@for (int i = 0; i < Model.TrackerKeys.Count(); i++)
{ 
    @Html.GenerateImage(PageModes.Http, Model.TrackerKeys[i])
}

并且

@for (int i = 0; i < Model.TrackerKeys.Count(); i++)
{ 
    @Html.HiddenFor(model => model.TrackerKeys[i])
}

这位于表单内 - 当表单提交时,如果发生验证错误,则 TrackerKeys 属性将更新为一组新的随机数字。我将先前的列表传回以确保用户不会再次看到相同的图像。

跟踪器键已正确设置并传递回视图。

我的问题是:

图像根据列表中的新值正确显示新图像

然而

隐藏字段的值不会更新为新值 - 它们保留原始值。

有任何想法吗?这是 MVC3 的错误吗?任何投入将不胜感激。谢谢你。

编辑

提交前的html:

<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/26.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/33.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/8.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/30.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/6.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/18.gif" width="35" />  

<input id="TrackerKeys_0_" name="TrackerKeys[0]" type="hidden" value="26" />
<input id="TrackerKeys_1_" name="TrackerKeys[1]" type="hidden" value="33" />
<input id="TrackerKeys_2_" name="TrackerKeys[2]" type="hidden" value="8" />
<input id="TrackerKeys_3_" name="TrackerKeys[3]" type="hidden" value="30" />
<input id="TrackerKeys_4_" name="TrackerKeys[4]" type="hidden" value="6" />
<input id="TrackerKeys_5_" name="TrackerKeys[5]" type="hidden" value="18" />

在帖子之后:在此处更正新值...

<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/16.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/20.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/11.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/19.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/26.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/15.gif" width="35" /> 

并且...仍然保留旧值...

<input id="TrackerKeys_0_" name="TrackerKeys[0]" type="hidden" value="26" />
<input id="TrackerKeys_1_" name="TrackerKeys[1]" type="hidden" value="33" />
<input id="TrackerKeys_2_" name="TrackerKeys[2]" type="hidden" value="8" />
<input id="TrackerKeys_3_" name="TrackerKeys[3]" type="hidden" value="30" />
<input id="TrackerKeys_4_" name="TrackerKeys[4]" type="hidden" value="6" />
<input id="TrackerKeys_5_" name="TrackerKeys[5]" type="hidden" value="18" />

控制器动作

[HttpPost]
    public ActionResult EmployerRegistration(EmployerViewModel Model)
    {
        if (ModelState.IsValid)
        {
            //do bits here
        }

        Model.TrackerKeys = Helper.GenerateNewNumbers(Model.TrackerKeys); 
        return View(Model);
    }
4

1 回答 1

13

这是 MVC3 的错误吗?

哦不,这是设计使然,这就是所有 HTML 助手在 ASP.NET MVC 中的工作方式。HTML 助手(例如Html.TextBoxFor, Html.HiddenFor, ...)在绑定它们的值时首先查看 ModelState,然后查看 Model。因此,如果您打算在 POST 控制器操作中修改模型的某些属性,并且该属性是 POST 主体的一部分,则必须首先从 ModelState 中删除旧值:

[HttpPost]
public ActionResult EmployerRegistration(EmployerViewModel Model)
{
    if (ModelState.IsValid)
    {
        //do bits here
    }

    // we clear all values from the modelstate => this will ensure that
    // the helpers will use the values from the model and not those that
    // were part of the initial POST.
    ModelState.Clear();
    Model.TrackerKeys = Helper.GenerateNewNumbers(Model.TrackerKeys); 
    return View(Model);
}

或者,如果您不想清除整个 ModelState(因为这将删除所有值以及可能与之关联的任何相应的模型状态错误),您可以仅删除您实际修改的那些键:

[HttpPost]
public ActionResult EmployerRegistration(EmployerViewModel Model)
{
    if (ModelState.IsValid)
    {
        //do bits here
    }

    for (var i = 0; i < Model.TrackerKeys.Count; i++)
    {
        ModelState.Remove(string.Format("TrackerKeys[{0}]", i));
    }
    Model.TrackerKeys = Helper.GenerateNewNumbers(Model.TrackerKeys); 
    return View(Model);
}
于 2012-08-17T17:12:23.247 回答