Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 701893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:39:38+00:00 2026-05-14T03:39:38+00:00

I have a .NET 2.0 class the properties of which are marked virtual.I need

  • 0

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

// .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; }
   }
}

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.

What am I missing, or is it only possible to do this using buddy classes.

Thanks.

EDIT 1:
I have now tried this with buddy validation classes and that doesn’t work either.

EDIT 2:
I have now worked out that the lambda expression supplied to the HtmlFor helpers is causing the problem. For e.g.

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.

I have tried changing the lambda expression to Html.TextBoxFor((MVCCustomer m) => m.FirstName) but the DeclaringType is still Customer.

Is there a way I can get the DeclaringType to be of type MVCCustomer and not Customer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:39:38+00:00Added an answer on May 14, 2026 at 3:39 am

    I have now got around this by using the new keyword on the properties in the .net 3.5 class as below

    // .NET 2.0 class
    public class Customer { 
      private string _firstName = "";
      public string FirstName
      {
         get { return _firstName; }
         set { _firstName = value; }
      }
    }
    
    // .NET 3.5 class
    public class MVCCustomer : Customer {
       [Required(ErrorMessage="Firstname is required")]
       public new string FirstName 
       { 
          get { return base.FirstName; } 
          set { base.FirstName = value; }
       }
    }
    

    This now works as expected and the DataAnnotations attributes from the MVCCustimer class are applied correctly.

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ASP.NET MVC 3 web application I have a viewmodel with properties which marked
I have created an ASP.NET class derived from the standard WebControls.TextBox , with the
I have copied class from net which inherits UIImageView. How to put that class
I have a .NET PropertyGrid control which displays properties of some class. I want
I have a MustInherit .NET class which declares a constructor with an integer parameter.
I am working in asp.net MVC2 application and I have question related to design
I have a vb.net application which runs as a service. I also have another
i have asp.net mvc application where my model has a relation like Question can
I have an ASP.NET MVC 2 project in which I've created a data transfer
I have a VB.NET class which I'm serializing via XML in an asmx file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.