我有以下表格:
<form action="~/buy-online" method="get" class="buy-online-form clearfix" autocomplete="off" id="buy-online-search">
<div class="infield-holder clearfix">
@Html.LabelFor(m => m.CustomerPostcode, new { @class = "infield" })
@Html.TextBoxFor(m => m.CustomerPostcode, new { @class = "textbox" })
<input type="submit" value="Buy Online" id="find-retailer-button" class="button" />
</div>
</form>
@Html.ValidationMessageFor(m => m.CustomerPostcode)
在没有 jQuery 的情况下提交时工作正常并且会显示错误消息,但是当我添加 jQuery 验证脚本 (v 1.11.1) 时:
<script src="/scripts/jquery.validate.js"></script>
<script src="/scripts/jquery.validate.unobtrusive.js"></script>
它停止提交表单但不显示错误消息
我的属性(property)是这样标记的:
[DisplayName("Enter your full postcode")]
[Required(ErrorMessage = "Please enter your full postcode")]
public string CustomerPostcode { get; set; }
HTML 呈现如下:
<input class="textbox" data-val="true" data-val-required="Please enter your full postcode" id="CustomerPostcode" name="CustomerPostcode" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="CustomerPostcode" data-valmsg-replace="true"></span>
如果我在点击提交时检查输入,它会将类 input-validation-error
添加到文本框,但不会更新验证消息。
我检查过的关于这个问题的所有帖子都只是说要包含脚本,所以我不知道为什么消息没有显示。
我什至尝试添加 jquery.unobtrusive-ajax.js 脚本,但这似乎也没有做任何事情。任何帮助将不胜感激,谢谢。
最佳答案
您需要在 jquery.validate.unobtrusive
的表单标签中包含 @Html.ValidationMessageFor(m => m.CustomerPostcode)
才能工作。
关于asp.net-mvc - Html.ValidationMessageFor 根本不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26674169/