2012-05-29 2 views
0

Visual Studio 2008과 함께 ASP.NET MVC 2를 사용하고 견해를 강하게 입력합니다. ValidationAnnotation을 사용하는 유효성 검사가 작동합니다.
내가 찾으려고하는 것은 양식을 열 때 유효성 검사를 시작하는 방법입니다. 열 때 모델에 오류가 있지만 오류가 표시되지 않습니다. 제출 버튼을 누르면 컨트롤러가 모델의 유효성을 검사하고 양식으로 돌아갑니다.asp.net mvc 렌더링하기 전에 객체의 유효성 검사를 시작하는 방법

Public Function EditVente(ByVal pNoEnreg As Integer) As ActionResult 
      Dim dossierVente As VenteDansMedianePlus = model.Helper.selectDossierVente(pNoEnreg) 

     Return View(dossierVente) 
    End Function 

    Public Function enregistrerVente(ByVal pVente As VenteDansMedianePlus) As ActionResult 
     If ModelState.IsValid Then 
      model.Helper.updateDossierVente(pVente) 
      Return RedirectToAction("EditVente", "A009P003", New With {Key .pNoEnreg = pVente.noEnreg}) 
     Else 
      Return View("EditVente", pVente) 
     End If 

    End Function 

은 내가 editVente 기능에 ModelState.IsValid를 넣어하려고 노력하지만, 그것은 작동하지 않습니다.

제 질문은 뷰를 반환하기 전에 모델 유효성 검사를 시작하여 뷰에 오류 메시지가 표시되는 방법입니다.

+0

방금 ​​발견했습니다. http://stackoverflow.com/questions/5347827/validation-messages-are-displayed-when-page-load. 그 사람은 내 해결책이 있습니다. – fanfaron

답변

0

여러분이 TryUpdateModel-method를 찾고 있다고 생각합니다. When and why do you use TryUpdateModel in asp.net mvc 2?

: SO 후이를 확인, 자세한 내용은

Public Function EditVente(ByVal pNoEnreg As Integer) As ActionResult 
    Dim dossierVente As VenteDansMedianePlus = model.Helper.selectDossierVente(pNoEnreg) 

    'Update the validation 
    TryUpdateModel(dossierVente) 

    Return View(dossierVente) 
End Function 

(미안 해요, C#을 개발자있어이 올바른지 희망) : 당신은 당신의 컨트롤러에이 메소드를 호출하고 모델에 전달할 수 있습니다 TryUpdateModel하지 않는 경우 유효성 검사가 실패 할 때 UpdateModel 오류가 발생합니다 제외 편집

TryUpdateModel-방법은 UpdateModel-방법과 동일 대해 작동합니다.

+0

안녕하세요, 시도했지만 내 개체를 tryUpdateModel에서 사용할 수 없다는 오류가 발생합니다 ... 그러나 나는 updateModel을 사용하고 tryUpdateModel을 사용하지 않는다는 것을 인정해야합니다. 나는 그것을 다시 시도 할 것이다. 프레임 워크가 쉽게 그렇게했다면 간단해야한다고 생각합니다. – fanfaron

+0

나는 updateModel을 호출하려고 노력할 것이다. 그러나 나는 그것을 시험해 보았다. 그리고 그것은 나에게 오류를 준다, 나는 내일 그것을 게시한다. – fanfaron

+0

이 발견되었습니다. http://stackoverflow.com/questions/9832742/validation-firing-on-page-load. 가짜 매개 변수를 전달하면 모델 바인딩 메커니즘이 트리거됩니다. – fanfaron

관련 문제