使用 jQuery.Validation.js 时提交按钮不发布数据

submit button doesn't post data while using jquery.validation.js

提问人:Atefe Rj 提问时间:6/17/2021 最后编辑:Yiyi YouAtefe Rj 更新时间:6/18/2021 访问量:92

问:

为了使用远程验证,C# Asp .net core,我添加了这两个库

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-val[email protected]/dist/jquery.validate.js"></script>

但这些停止提交按钮和发布数据。 我的模特道具:

[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Captions))]
[Display(Name = "Username", ResourceType = typeof(Titles))]
[Remote("ValidateUsername", "SignUp")]
[RegularExpression("^[a-zA-Z_]*$", ErrorMessageResourceName = "Wrong_Format", ErrorMessageResourceType = typeof(Captions))]
public string Username { get; set; }

在控制器中:

  [HttpPost]
    public async Task<IActionResult> Index(SignUpViewModel model)
    {
        if (!ModelState.IsValid) return View(model);
        var user = _mapper.Map<User>(model);
        var addUserResult = await _userManager.CreateAsync(user, model.Password);
        if (addUserResult.Succeeded)
        {
            var addRoleResult = await _userManager.AddToRoleAsync(user, RoleNames.Client);
            if (addRoleResult.Succeeded)
            {
                var code = GenerateValidationCode();
                bool sendSmsResult = _sendSms.SendSignUpValidationCode(model.PhoneNumber);
                if (sendSmsResult) return RedirectToAction("ValidationCode");
            }
        }
        return View(model);
    }


//---Remote Action----
        public async Task<IActionResult> ValidateUsername(string username)
        {
            var user = await _userManager.FindByNameAsync(username);
            if (user != null) return Json(data: Messages.Username_Already_Taken);
            return Json(data: true);
        }

在Razor页面中,我有:

<form method="post" id="signup" asp-action="Index" asp-controller="SignUp">
        <div class="text-right mt-3">
            @Html.LabelFor(r => r.Username)
            <input type="text" id="username" asp-for="Username" class="form-control form-log-in">
            @Html.ValidationMessageFor(r => r.Username, "", new {@class = "error"})
        </div>
        <div class="form-group">
            <button type="submit" class="btn see-more-btn px-3 py-1 text-dark text-center text-decoration-none w-100 d-block">ثبت نام</button>
        </div>
    </form>

远程验证效果很好,提交时会对库进行注释。

asp.net-mvc asp.net-core 表单提交 unobtrusive-validation 远程验证

评论

0赞 Yiyi You 6/18/2021
您需要在 .您的主机中是否有任何错误?query.validate.jsjquery.validate.unobtrusive.min.js
0赞 Atefe Rj 6/19/2021
是的!!我对另一个未实现操作的字段使用了另一个远程验证器,它阻止了发布数据。谢谢:)

答: 暂无答案