Search code examples
asp.net-mvcvalidationrazormessagedisplay

not show ValidationMessageFor in Razor


I write in Asp.net Mvc code.

The ValidationMessageFor message is not displayed to me. I've added scripts to html but still does not work. Pls help me.

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>

Solution

  • Might be this missing in your web.config?

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

    "Because no JavaScript is emitted when you use unobtrusive client validation, if you forget to include the validation scripts, you will not see any errors when the page loads. The only result is that the form values will not be validated in the browser."

    Source: How to: Implement Remote Validation in ASP.NET MVC