2

비슷한 질문이 전에 (Custom ValidationSummary template Asp.net MVC 3) 요청되었지만 두 가지 대답 중 하나도 내가 가진 추가 요구 사항을 만족시키지 못합니다. 즉, 클라이언트가 클라이언트 측 유효성 검사를 중단하지 않는다는 것입니다. 측면 검증.Custom ValidationSummary 템플릿

그래서, 사람이 같은 기능을 얻을 수있는 방법을 알고 않습니다

클라이언트 측 유효성 검사와 함께 작동
@if (!ViewData.ModelState.IsValid) 
{ 
    <div class="form-errors"> 
     <p>Please have another look at the fields highlighted below</p> 
     @Html.ValidationSummary() 
    </div> 
} 

?

클라이언트 측 유효성 검사가 꺼져있는 경우 즉, 모델이 유효하면 전체 div를 제외하고 오류가있는 경우 표시합니다. 그러나 조건문은 평가가 거짓 일 때 양식이 처음 렌더링 될 때 전체 섹션이 렌더링되지 않으므로 jquery.validate가 유효성 검사 요약을 삽입 할 수있는 곳을 찾을 수 없음을 의미합니다.

아이디어가 있으십니까?

답변

3

도우미 :

public static MvcHtmlString MyValidationSummary(this HtmlHelper helper, bool excludePropertyErrors = false) 
    { 
     string html = ""; 

     html += helper.ViewData.ModelState.IsValid ? "<div class='form-errors' style='display:none;'>" : "<div class='form-errors'>"; 

     html += "<p>Please have another look at the fields highlighted below</p>"; 
     html += helper.ValidationSummary(excludePropertyErrors); 
     html += "</div>"; 

     return new MvcHtmlString(html); 
    } 

보기 :

@using (Html.BeginForm("Index", "Home",FormMethod.Post, new {id="myform"})) 
{ 
    @Html.MyValidationSummary() 
    ... 
    <input type="button" id="submitbutton" value="Submit" /> 
} 

JS :

$("#submitbutton").click(function() { 
    $("#myform").validate(); 

    if (!$("#myform").valid()) { 
     $("#myform .form-errors").show(); 
    } 
    else { 
     $("#myform").submit(); 
    } 
}); 

업데이트 :

이 부분으로 이동하려면 보기

공유/_MyValidationSummary.cshtml

@if (!ViewData.ModelState.IsValid) 
{ 
    <div class="form-errors"> 
     <p>Please have another look at the fields highlighted below</p> 
     @Html.ValidationSummary() 
    </div> 
} 
else 
{ 
    <div class="form-errors" style="display:none;"> 
     <p>Please have another look at the fields highlighted below</p> 
     @Html.ValidationSummary() 
    </div> 
} 

보기 :

@using (Html.BeginForm("Index", "Home",FormMethod.Post, new {id="myform"})) 
{ 
    @Html.Partial("_MyValidationSummary") 
    ... 
    <input type="button" id="submitbutton" value="Submit" /> 
} 
+0

아주 좋은 방법, sormii입니다. 나는 html에 대한 실제 싫어함이 그런 문자열에 묻혀 있기 때문에 HTML 도우미를 부분 뷰로 전환하는 것은 쉽지 않다고 생각한다. – SimonF

+0

어쨌든 부분보기 접근법에 대한 답변이 업데이트되었습니다. –

+0

좋은 하나! 누군가가 더 우아한 해결책을 찾을 때까지 나는 진드기를 줄 것이다.) – SimonF

관련 문제