提问人:Atefe Rj 提问时间:6/17/2021 最后编辑:Yiyi YouAtefe Rj 更新时间:6/18/2021 访问量:92
使用 jQuery.Validation.js 时提交按钮不发布数据
submit button doesn't post data while using jquery.validation.js
问:
为了使用远程验证,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>
远程验证效果很好,提交时会对库进行注释。
答: 暂无答案
上一个:远程验证不适用于部分视图
下一个:远程验证命中功能两次
评论
query.validate.js
jquery.validate.unobtrusive.min.js