0

I am new to .net MVC. I am trying to validate a field from the model, but error message is not showing. Although the fields are red, the message just not show. Please see bellow :

The model:

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

The cshtml:

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

What am I missing?

1 Answer 1

1

Make sure you have added following scripts in the view

<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>

And enable validation from web config

<appSettings>
........
........
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Sign up to request clarification or add additional context in comments.

2 Comments

I have : <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.18.0/jquery.validate.min.js' type='text/javascript'> </script> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.js")' type='text/javascript'></script> and my 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>
Then you are missing jQuery lib, see the first script in my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.