不在 Razor 中显示 ValidationMessageFor

not show ValidationMessageFor in Razor

我写在Asp.net Mvc代码中。

ValidationMessageFor 消息没有显示给我。 我已将脚本添加到 html,但仍然不起作用。 请帮助我。

The piece of code is summarized from Html Code

@using (Html.BeginForm("****", "***", FormMethod.Post, new {area = "***"}))
{
            @Html.AntiForgeryToken()
            @Html.ValidationSummary()
            @Html.HiddenFor(model => model.PK_User)

            <div class="form-group">
                @Html.TextBoxFor(model => model.FirstName)
                @Html.ValidationMessageFor(model => model.FirstName)
            </div>
}

@section PageScript { 
 <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
 <script src="~/Scripts/jquery.validate.min.js"></script>
}

in Class

public class User
{
    [ScaffoldColumn(false)]
    [Bindable(false)]
    public long PK_User { get; set; }

    [Required(ErrorMessage = "Please Enter Name" , AllowEmptyStrings = false)]
    [StringLength(50, MinimumLength = 3, ErrorMessage = "Error Name"]
    public string FirstName { get; set; }
}

The piece of code is summarized from 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>

您的 web.config 中可能缺少这个吗?

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

"因为当您使用不显眼的客户端验证时不会发出 JavaScript,如果您忘记包含验证脚本,您将不会在页面加载时看到任何错误。唯一的结果是表单值不会在浏览器中验证。"

来源:How to: Implement Remote Validation in ASP.NET MVC