我正在使用MVC5
并使用客户端和服务器验证的组合。对于这个特殊问题,我正在使用远程验证。
Razorview :
<div id="studentStatus" name="studentStatus">
@using (Html.BeginForm("StudentStatus", "StudentStatus", FormMethod.Post, new { id = "studentStatusForm", data_timer = 240000 }))
{
@Html.HiddenFor(x => x.CurrentStudentSepId, new { @class = "hideMe" })
<div class="row">
<h2 class="col-md-12 topSectionHeader">
@StaffPortalResources.Section_StudentStatus
<span class="savedStatus"></span>
</h2>
</div>
<span class="errorNotification"></span>
<div class="questionGroup form-group">
<div class="row margin-btm-sm">
<div class="col-md-4">
@Html.LabelFor(x => x.StudentStatusId)
@if (Model.IsReadOnly)
{
<div>@Html.DisplayFor(x => x.StudentStatusId)</div>
@Html.HiddenFor(x => x.StudentStatusId)
}
else
{
@Html.CustomComboBoxFor(x => x.StudentStatusId, (new { @class = "statusChangeValidationHandler", data_action = "GetStudentEditableStatuses", data_controller = "Lookup" }))
}
@Html.ValidationMessageFor(x => x.StudentStatusId)
</div>
}
模特:
public sealed class StudentStatusSectionViewModel : SectionViewModel
{
[Display(Name = "Status", ResourceType = typeof(StaffPortalResources))]
[SelectMustExist(true, ErrorMessageResourceType = typeof (StaffPortalResources), ErrorMessageResourceName = "StudentStatus_InvalidSelection")]
[Remote("ValidateStudentStatus", "StudentStatus", AdditionalFields = "CurrentStudentSepId")]
public string StudentStatusId { get; set; }
public bool DoNotReenroll { get; set; }
}
控制器:
public JsonResult ValidateStudentStatus(StudentStatusSectionViewModel model)
{
var errors = _studentStatusSectionAdapter.ValidateStudentStatus(model.CurrentStudentSepId, model.StudentStatusId);
if (errors != null && errors.Any())
{
var currentStatus = _studentStatusSectionAdapter.Get(model.CurrentStudentSepId).StudentStatusId;
Response.AddHeader("status", currentStatus);
return Json(string.Join("</br>", errors), JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}
远程验证.complete()函数:
scope.initializeStudentStatusDropdown = function() {
var $element = $('#studentStatusForm').find('input.statusChangeValidationHandler[data-role="combobox"]');
$element.rules().remote.complete = function (xhr) {
if (xhr.responseText !== 'true')
window.Registration.StudentStatus.fixStudentStatusDropdown($element, xhr.getResponseHeader);
}
}
scope.fixStudentStatusDropdown = function($element,responseHeader) {
$element.kVal(responseHeader('status'));
}
用于在更改事件上进行验证的jquery:
scope.initializeAutoSave = function (form, callBack) {
var $form = $(form);
$form.on("change", "input:not(.ignoreAutoSave)", queueForm);
$form.on("change", "textarea:not(.ignoreAutoSave)", queueForm);
window.Validation.initializeValidator($form);
callBacks[$form.attr('id')] = callBack || function() {};
}
queueForm方法:
function queueForm(e) {
if ($(e.target).valid()) {
var $form = $(e.target).closest("form");
var timer = $form.data("timer");
autoSaveQueue[$form.attr("id")] = { $form: $form, timer: timer, attempt: 0 };
}
}
在更改下拉列表中的值后跟踪代码时,change事件将触发并调用queueForm函数。在远程验证发生之前,valid()
调用的计算结果为true。
直到我跳过'if ($(e.target).valid())
'行后,它才会计算为true并进入if块。然后它跳转到我的控制器中的远程ValidateStudentStatus
函数。
我在这里做错了吗?我误解了远程验证的工作原理吗?
非常感谢任何见解或帮助!