2012-08-17 3 views
3

숨겨진 필드 목록 :MVC3 모델 바인딩 - 나는 독특한 문제가

@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

+0

컨트롤러 작업을 게시 할 수 있습니까? – nemesv

+0

게시자가 컨트롤러에 조치를 취합니다. – Carl

답변

13

이 MVC3 버그인가?

아뇨, 그건 설계 상으로는 모든 HTML 도우미가 ASP.NET MVC에서 작동하는 방식입니다. HTML 도우미 (예 : Html.TextBoxFor, Html.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); 
} 
+0

감사합니다. 매일 새로운 것을 배웁니다. – Carl

+0

언제나처럼 ... 아주 간단하고 간단하게 설명합니다 ... 대린에게 감사드립니다. – shazia