MVC5-JQuery不引人注目的客户端验证不起作用
我正在开发一个MVC Web应用程序(从MVC4升级到MVC5),但客户端验证无法正常工作。所以为了排除这些问题,我创建了一个默认的空MVC应用程序 不幸的是,我仍然无法正常工作,contactform一直提交到服务器,afterworths显示来自服务器的错误消息,因为viewmodel无效 这是我的密码 Contact.cshtmlMVC5-JQuery不引人注目的客户端验证不起作用,jquery,asp.net,asp.net-mvc,unobtrusive-validation,asp.net-mvc-5.2,Jquery,Asp.net,Asp.net Mvc,Unobtrusive Validation,Asp.net Mvc 5.2,我正在开发一个MVC Web应用程序(从MVC4升级到MVC5),但客户端验证无法正常工作。所以为了排除这些问题,我创建了一个默认的空MVC应用程序 不幸的是,我仍然无法正常工作,contactform一直提交到服务器,afterworths显示来自服务器的错误消息,因为viewmodel无效 这是我的密码 Contact.cshtml @model BLL.DTO.ContactDTO @{ ViewBag.Title = "Contact"; } <h2>@ViewBag
@model BLL.DTO.ContactDTO
@{
ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
@using (Html.BeginForm("Contact","Home", FormMethod.Post, new {id = "contactform"}))
{
@Html.LabelFor(x => x.Firstname)
@Html.TextBoxFor(x => x.Firstname)
@Html.ValidationMessageFor(x => x.Firstname)
<input type="submit" value="Verzenden"/>
}
<address>
One Microsoft Way<br />
Redmond, WA 98052-6399<br />
<abbr title="Phone">P:</abbr>
425.555.0100
</address>
<address>
<strong>Support:</strong> <a href="mailto:Support@example.com">Support@example.com</a><br/>
<strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address>
@*@section scripts {
<script type="text/javascript">
$("#contactform").validate({
debug: false
});
</script>
}*@
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@*@Scripts.Render("~/bundles/jqueryval")*@
@Scripts.Render("~/bundles/msval")
@RenderSection("scripts", required: false)
</body>
BundleConfig.cs
public class ContactDTO : BaseDTO
{
[Required (ErrorMessage = ErrorConstants.ThisFieldIsRequired)]
[Display(Name = "Voornaam")]
[MaxLength(75)]
public string Firstname { get; set; }
[Required(ErrorMessage = ErrorConstants.ThisFieldIsRequired)]
[Display(Name = "Achternaam")]
[MaxLength(75)]
public string Lastname { get; set; }
[Display(Name = "E-mail")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Display(Name = "Website")]
public string Website { get; set; }
[Required(ErrorMessage = ErrorConstants.ThisFieldIsRequired)]
[Display(Name = "Opmerkingen")]
[DataType(DataType.Text)]
public string Comments { get; set; }
}
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
bundles.Add(new ScriptBundle("~/bundles/msval").Include(
"~/Scripts/jquery.validate.unobtrusive.js"));
web.config
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
,和)虽然问题看起来很相似,但它并不能解决我的问题。这可能是什么?在我的例子中,由于我的类库名称空间和默认模型名称空间不匹配,验证无法工作。
我已经纠正了它,现在它工作正常。您需要验证和验证不引人注目。如果只包含~/bundles/jqueryval
,则会发生这种情况,因为它的掩码与query.validate.unobtrusive
匹配。如果您同时包含这两个包,那么使用它们当前的掩码,您将包含两次unobtrusive
。您的控制器在哪里?您还需要向我们展示您的控制器,让我们知道您是否处理了传递给视图的模型是否有效。Un-comment@Scripts.Render(~/bundles/jqueryval))
所以它被包含并注释掉了@Scripts.Render(“~/bundles/msval”)
@GSerg,thnx就是它。。现在似乎正在我的测试应用程序中工作。。将对正常应用程序进行相同的更改