The Muffin Man
The Muffin Man

Reputation: 20004

Data annotations not working in view model

I have a few different models with properties that I've decorated with data annotations for validation.

public class BillingModel
{
    [Required,
    DisplayName("First Name")]
    public string FirstName { get; set; }

    [Required,
    DisplayName("Last Name")]
    public string LastName { get; set; }
}

public class CustomerModel
{
    [Required,
    DisplayName("Address")]
    public string Adress { get; set; }

    [Required,
    DisplayName("City")]
    public string City { get; set; }
}

When I put them in a view model like this:

public class OrderViewModel
{
    public BillingModel Billing { get; set; }
    public CustomerModel Customer { get; set; }
}

They render out like this:

<input id="Business_FirstName" name="Business.FirstName" type="text" value="" />

<input id="Business_LastName" name="Business.LastName" type="text" value="" />

My Razor looks like this:

@Html.TextBoxFor(x => x.Business.FirstName)
@Html.TextBoxFor(x => x.Business.LastName)

I have many properties that need to live in their own classes as each class contains specific methods. Even if I put [Required] on each property in the View Model it still doesn't work.

Upvotes: 5

Views: 18661

Answers (4)

Adil Husain
Adil Husain

Reputation: 1

There is one more case: You have not added the reference of
System.ComponentModel.DataAnnotations in your current project layer.

TRY THIS: Navigate → Project(Top Left Corner) → Add Reference → System.ComponentModel.DataAnnotations
Select the Checkbox and Wallah!:D

image for reference

Upvotes: 0

Fryann Martinez
Fryann Martinez

Reputation: 408

Once you put the required Data Annotation in your Field in the View Models, for example rendered source of class Persona

And, if it doesn't work, just write this in the section script in your View page that you are using the ViewModel like this: redundant rendered <script /> elements
I hope that can help you!
And don't forget to use the tag decorator below to the field @Html.ValidationMessageFor(x => x.Model.Name)

here is the scripts :

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

Upvotes: 3

MartinDotNet
MartinDotNet

Reputation: 2505

Just for anyone else who comes across this, in addition to the other answer, you also need to have the following web.config settings enabled:

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

This is what enables the data annotations to have an affect on the rendered html.

Upvotes: 7

user596075
user596075

Reputation:

You also need to put the following <script /> elements in your view (preferably the _layout.cshtml View if you will be using client side validation across all views):

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

Modify the paths accordingly. But that should get your validation up and running.

Also, use the ValidationMessageFor() in your view. Like so:

@Html.TextBoxFor(x => x.Business.FirstName)
@Html.ValidationMessageFor(x => x.Business.FirstName)

@Html.TextBoxFor(x => x.Business.LastName)
@Html.ValidationMessageFor(x => x.Business.LastName)

Upvotes: 9

Related Questions