2012-08-17 78 views
3

我有一個特殊的問題 - 我有一個視圖模型有一個列表,它用於顯示圖像的列表: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); 
    } 
+0

你能提供生成的HTML ?我懷疑你的html元素沒有相同的名稱/ id,它不能將它識別爲列表(如果它甚至可以)。 – Joakim 2012-08-17 16:32:41

+0

你能發佈你的控制器動作嗎? – nemesv 2012-08-17 16:37:31

+0

我會把控制器的動作發到帖子 – Carl 2012-08-17 16:45:00

回答

13

這是MVC3的錯誤嗎?

哦不,這是所有的HTML helper在ASP.NET MVC中的工作原理。 HTML幫助器(如Html.TextBoxFor,0123,,...)在綁定它們的值時先查看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(因爲這將刪除所有值,並可能被關聯到他們的任何相應的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); 
} 
+0

謝謝完全正確!你每天學習新的東西。 – Carl 2012-08-17 17:21:06

+0

一如既往的最佳和簡單的解釋......感謝Darin。 – shazia 2013-12-21 00:21:24