3

独特の問題があります-画像のリストを表示するために使用されるリストを含むViewModelがあります:

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

これは、ページ内の 2 つの場所で使用されます。

@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 のバグですか?

これは設計によるものであり、ASP.NET MVC ですべての HTML ヘルパーが動作する方法です。HTML ヘルパー ( Html.TextBoxForHtml.HiddenFor、 ... など) は、値をバインドするときに最初に ModelState を調べ、その後でモデルを調べます。したがって、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 に答える