我有一個特殊的問題 - 我有一個視圖模型有一個列表,它用於顯示圖像的列表:MVC3模型綁定 - 列表以隱藏字段
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])
}
這是坐在一個表單中 - 當提交表單時,如果出現驗證錯誤,日e 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);
}
你能提供生成的HTML ?我懷疑你的html元素沒有相同的名稱/ id,它不能將它識別爲列表(如果它甚至可以)。 – Joakim 2012-08-17 16:32:41
你能發佈你的控制器動作嗎? – nemesv 2012-08-17 16:37:31
我會把控制器的動作發到帖子 – Carl 2012-08-17 16:45:00