Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC2 DataAnnotations validation with inheritance
    primarykey
    data
    text
    <p>I have a .NET 2.0 class the properties of which are marked virtual.I need to use the class as a model in a MVC2 application. So, I have created a .NET 3.5 class inheriting from the .NET 2.0 class and added the DataAnnotations attributes to the overriden properties in the new class. A snippet of what I have done is below</p> <pre><code>// .NET 2.0 class public class Customer { private string _firstName = ""; public virtual string FirstName { get { return _firstName; } set { _firstName = value; } } } // .NET 3.5 class public class MVCCustomer : Customer { [Required(ErrorMessage="Firstname is required")] public override string FirstName { get { return base.FirstName; } set { base.FirstName = value; } } } </code></pre> <p>I have used the class as the model for a MVC2 view using the HtmlFor helpers. Serverside validation works correctly but the client side validation does not. Specifically the validation error is not displayed on the page. </p> <p>What am I missing, or is it only possible to do this using buddy classes.</p> <p>Thanks.</p> <p>EDIT 1: I have now tried this with buddy validation classes and that doesn't work either.</p> <p>EDIT 2: I have now worked out that the lambda expression supplied to the HtmlFor helpers is causing the problem. For e.g.</p> <p>Html.TextBoxFor(m => m.FirstName) calls the ModelMetadata.FromLambdaExpression method which evaluates the DeclaringType of the MemberExpression (expression.Body) as the Customer class and not the MVCCustomer class.</p> <p>I have tried changing the lambda expression to Html.TextBoxFor((MVCCustomer m) => m.FirstName) but the DeclaringType is still Customer.</p> <p>Is there a way I can get the DeclaringType to be of type MVCCustomer and not Customer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload