2

我有一个基于 viewModel 数据的视图,并带有一些远程验证(3 个带有远程验证的字段)。

问题是,当我提交表单时,所有 3 个远程验证都会在提交的同时触发(见下文)。

在此处输入图像描述

所以你在上图中看到,所有 3 个远程验证都是同时触发的,并且处于待处理状态。1分钟后我有一个超时。

这是我的代码:

var $dialog = $('#modal-dialog');
$dialog.modal('show');
// Enable client side validation
$.validator.unobtrusive.parse(this);
// Setup the ajax submit logic
submitLogic(params);

function submitLogic(params) {
    var $form = $('#modal-dialog form');
    $form.submit(function () {

        // Do not submit if the form does
        // not pass client side validation
        if (!$(this).valid()) {
            writeError('msgError', pDialogForm.validationFailed);
            return false;
        }

        clearErrors();

        $.post($(this).attr('action'), $(this).serialize(), function (data, status) {
            // Post success
            $('#modal-dialog').modal('hide');
        }).error(function (error, status, a, b) {
            // Post failed
            writeError('msgError', pDialogForm.validationFailed);
            // Need to reactivate client side validation
            $.validator.unobtrusive.parse($form);
            // Need to rebind submit event
            submitLogic();
        });
        // Unbind form submitting
        $form.unbind();
        return false;
    });
}

我的远程验证代码:

    public ActionResult EmailAlreadyExists(string email, int? contactID, ContactTypeEnumViewModel type )
    {
        var contact = _requestServiceClient.GetContactFromEmailAndType(email, (ContactTypeEnumDTO) type);
        return Json((contact == null) || (contact.ContactID == contactID), JsonRequestBehavior.AllowGet);
    }

    public ActionResult NameAlreadyExists(int? contactID, ContactTypeEnumViewModel type, string FirstName, string LastName)
    {
        var contact = _requestServiceClient.GetContactFromFirstNameLastNameAndType(FirstName, LastName, (ContactTypeEnumDTO)type);
        return Json((contact == null) || (contact.ContactID == contactID), JsonRequestBehavior.AllowGet);
    }

我的视图模型:

public class ContactEditViewModel
{
    public int ContactID { get; set; }

    [Required, Remote("NameAlreadyExists", "Contact", AdditionalFields = "ContactID, Type, FirstName, LastName", ErrorMessageResourceName = "NameAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
    public string FirstName { get; set; }

    [Required, Remote("NameAlreadyExists", "Contact", AdditionalFields = "ContactID, Type, FirstName, LastName", ErrorMessageResourceName = "NameAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
    public string LastName { get; set; }

    [Required, EmailRegex, Remote("EmailAlreadyExists", "Contact", AdditionalFields = "ContactID, Type", ErrorMessageResourceName = "EmailAlreadyExists", ErrorMessageResourceType = typeof(UserResource))]
    public string Email { get; set; }
    ...

我很迷茫:(我试图找到一个解决方案,因为几天没有成功。非常感谢任何帮助。

祝你今天过得愉快。

4

0 回答 0