TDaver
TDaver

Reputation: 7264

MVC Validation with ViewModel

I have EF4 entities, decorated with DataAnnotations, I have my clientside validation enabled in web.config, and everything. It works, except on some views my @model is a MyViewModel where T is one of the EF4 entities:

public class MyViewModel<T>
{
 public T Entity {get;set;}
 public string SomeOtherPropertyWhichDoesntNeedValidation {get;set;}
}

Now for some reason validations on T doesn't work when using in a ViewModel. Just to clarify, the textboxes are generated by @Html.TextBoxFor(o=>o.Entity.Title); etc.

please help

EDIT: This is how my entity looks like

[MetadataType(typeof(TextMeta))]
public partial class Text
{
        class TextMeta
        {
            [Required(ErrorMessage="This is required!!!")]
            public string Title { get; set; }
        }
}

Upvotes: 1

Views: 639

Answers (1)

chris
chris

Reputation: 37480

Can you try this?

[MetadataType(typeof(TextMeta))] 
public partial class Text {}

public class TextMeta 
{
    [Required(ErrorMessage="This is required!!!")]
    public string Title { get; set; } 
}

Upvotes: 1

Related Questions