2

我正在MVC5使用客户端和服务器验证的组合。对于这个特定问题,我正在使用远程验证。

剃刀视图:

<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 };
    }
}

在更改下拉列表中的值后跟踪代码时,会触发更改事件并调用 queueForm 函数。在valid()远程验证发生之前,调用的计算结果为 true。

直到我跨过“if ($(e.target).valid())”行之后,它才评估为 true 并进入 if 块。然后它跳转到我的控制器中的远程ValidateStudentStatus功能。

我在这里做错了吗?我是否误解了远程验证的工作原理?

任何见解或帮助将不胜感激!

4

0 回答 0