2

내가 기본 양식을 등

@Html.ValidationSummary(true) 
using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form" })) 
{ 
    <div class="editor-label"> 
     <label for="Name">@Html.LabelFor(model => model.Name)</label> 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Name) 
    </div> 

및이 작동하지 hijax. 내 모델에서 데이터 주석을 사용하고 있으며 유효성 검사가 설정되어 있습니다.

좀 jQuery를 사용하여 양식을 hijaxing 해요 : 내가 제대로 제출의 필드를 입력하면

<script type="text/javascript"> 
    $("form[action='/']").submit(function() { 
     $.post($(this).attr("action"), $(this).serialize(), function (response) { 
      $("#contactForm").replaceWith($("#contactForm", response)); 
     }); 
     return false; 
    }); 
</script> 

이 작동합니다. 하지만 이상한 것은 양식 필드 중 하나에 유효하지 않은 값을 입력하고 유효성 검사 코드가 내 실수를 강조 표시하는 경우입니다. 위의 hijaxing 스크립트에 추가 된 이벤트 기능을 삭제하면 전체 게시물이 발생합니다.

내가이 문제를 어떻게 해결할 수 있는지에 대한 좋은 아이디어가 있습니까?

답변

3

양식에 제출 처리기를 추가하면 jquery 유효성 검사/데이터 주석이 추가하는 제출 처리기를 덮어 쓰는 것으로 생각됩니다.

은 추가 할 수있는이 문제를 효율적으로 활용하려면 다음

var theForm = $(this); 
if (theForm.valid()) { 
    $.post($(this).attr("action"), $(this).serialize(), function (response) { 
     $("#contactForm").replaceWith($("#contactForm", response)); 
    }); 
    return false; 
} 

이 양식이 POST를하기 전에 유효한지 확인합니다.

+0

감사합니다. 그것은 문제가되는 것 같습니다. –

+0

도움이된다면 답을 표시하십시오. – Dismissile

+0

@Dismissile Im 당신이 제출을 구부리고있는 곳을 보지 못했습니까? 내가 놓친 무언가, 바로 긴 하루였습니다.) –

관련 문제