MSAjax MVC 3 면도기 뷰에서 생성 된 양식에 jQuery 아약스 게시물을 사용했습니다.MVC 3 POST 후 클라이언트 측 유효성 검사가 중지됩니다.
이 양식에서는 입력하는 동안 클라이언트 측 유효성 검사가 올바르게 작동하지만 사용자가 "검색"버튼을 클릭하면 "POST"가 생성되면 클라이언트 측 유효성 검사가 중지됩니다 !!
public class AnagraficaSearchViewModel : ViewModelBase
{
[Required]
[StringLength(2)]
public virtual string Ricerca { get; set; }
}
이것은 컨트롤러 :
이
모델입니다[HttpPost]
public ActionResult Index(AnagraficaSearchViewModel model)
{
if (model.Ricerca == "12")
{
model.Status = Status.Error;
model.Message = "Errore!";
ModelState.AddModelError("", Cult.Testo(Cult.CultTextsIDs.StatusError));
}
if (Request.IsAjaxRequest()) return PartialView("SearchCriteria", model);
return View(model);
}
이 메인이다 :
<div id="search-anagrafica-criteria">
@{ Html.RenderPartial("SearchCriteria", Model); }
</div>
그리고 이것은 SearchCriteria보기입니다 :
@model AnagraficaSearchViewModel
<script type="text/javascript">
function CheckRow() {
$("form").valid();
abs.message.convertErrorsToTooltips();
};
function InitSearch() {
$("#Ricerca").bind("propertychange keyup input paste", function() {
setTimeout("CheckRow();", 100);
});
CheckRow();
$("#Ricerca").focus();
}
$(function() {
$('form').submit(function (e) {
e.preventDefault();
var $form = $(this);
if (!$form.valid()) {
abs.message.convertErrorsToTooltips();
return false;
}
$.validator.unobtrusive.parse($('form')); //added
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('#search-anagrafica-criteria').html(result);
InitSearch();
},
complete: function (result) {
CheckRow();
}
});
}
return false;
});
InitSearch();
});
</script>
@using (Ajax.BeginForm("Index", "Anagrafica", null, new AjaxOptions { UpdateTargetId = "search-anagrafica-criteria", HttpMethod = "POST", OnComplete = "CheckRow()" }))
{
@Html.LabelFor(el => el.Ricerca)
@Html.TextBoxFor(el => el.Ricerca)
@Html.ValidationMessageFor(el => el.Ricerca)
<button class="btn btn-primary" type="submit" style="margin-left: 10px">Search</button>
@Html.ValidationSummary(true)
}
많은 작업 시간과 변화 없음 !!
도움 주셔서 감사합니다. 다음 대신
당신이 $ .validator.unobtrusive.parse을 ($ ('양식'))를 제거하려고 했습니까; 에서 내 게시물을 참조하십시오 원래의 위치에서부터 모든 뷰 코드 다음에 배치하십시오. 즉, AjaxForm parantesis를 닫기 직전에 다음 코드를 추가하십시오. – NiK
데이터 요소를 먼저 제거하지 않아도 위치를 옮겨도 구문 분석 메서드는 눈에 거슬리지 않는 유효성 검사를 다시 바인딩하지 않습니다. –