验证消息未显示.Net MVC

时间:2018-11-25 03:37:19

标签: asp.net-mvc-4

我是.net MVC的新手。我正在尝试验证模型中的字段,但未显示错误消息。尽管这些字段是红色的,但该消息只是不显示。请参阅以下内容:

模型:

[Required(AllowEmptyStrings = false, ErrorMessage = "FirstName is required")]
[Display(Name = "First Name")]
public string FirstName { get; set; }

cshtml:

<div class="editor-field">
  @Html.EditorFor(model => model.FirstName)
  @Html.ValidationMessageFor(model => model.FirstName)
</div>

我想念什么?

1 个答案:

答案 0 :(得分:1)

确保在视图中添加了以下脚本

<script src='@Url.Content("~/Scripts/jquery-1.8.2.js")' type='text/javascript'></script>    
<script src='@Url.Content("~/Scripts/jquery.validate.js")' type='text/javascript'>         </script>
<script src='@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")' type='text/javascript'></script>

并通过Web配置启用验证

<appSettings>
........
........
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>