没有表单提交的MVC表单验证
我有一个标准表格,由MVC模型填充,标准[必需]验证。我想通过AJAX提交此表单数据,而不是通过提交,我想利用内置的MVC / razor验证功能。我无法弄清楚如何在不触发表单提交事件的情况下触发客户端验证。
I have a standard form populated by an MVC model with standard [Required] validation. I want to "submit" this form data via AJAX, not by a submit, and I would like to take advantage of the built-in MVC/razor validation features. I can't figure out how to fire the client-side validation without triggering the form submit event.
这是我的剃刀标记:
@using (Html.BeginForm()) {
<span class="label">Team Name:</span> @Html.TextBoxFor(m => m.Name})
@Html.ValidationMessageFor(m => m.Name)
这是我的模特:
public class Team
{
[Required(ErrorMessage = "Required")]
public string Name { get; set; }
看起来这应该是一件容易的事情。它在提交时效果很好。我只需要知道如何手动调用验证方法。
It seems like this should be an easy thing to do. It works beautifully on submit. I just need to know how to invoke the validation method manually.
此功能默认启用,但尚未启用因为您可能没有添加指向所需JavaScript库的链接而工作。
This feature is enabled by default, but it has not been working because you might not have added links to the required JavaScript libraries.
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>