user4148098
user4148098

Reputation:

Validation message not showing .Net MVC

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?

Upvotes: 0

Views: 44

Answers (1)

Ambrish Pathak
Ambrish Pathak

Reputation: 3968

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>

Upvotes: 1

Related Questions