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 6819575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:19:22+00:00 2026-05-26T21:19:22+00:00

I have classes like the following. That is many classes with the same few

  • 0

I have classes like the following. That is many classes with the same few properties such as CreatedBy and ModifiedBy:

public class Test1 {
    [DisplayName("Created By")]
    public string CreatedBy { get; set; }
    [DisplayName("Modified By")]
    public string ModifiedBy { get; set; }
}
public class Test2 {
    [DisplayName("Created By")]
    public string CreatedBy { get; set; }
    [DisplayName("Modified By")]
    public string ModifiedBy { get; set; }
}

I would like to have a block of code that I reuse in different views for these classes. The block of the code should have formatting for the data such as:

        <div class="a">
            @Html.LabelFor(model => model.Note.CreatedBy)
            @Html.TextBoxFor(model => model.Note.CreatedBy)
        </div>
        <div class="b">
            @Html.LabelFor(model => model.Note.ModifiedBy)
            @Html.TextBoxFor(model => model.Note.ModifiedBy)
        </div>

I looked at a few different ways of doing this and initially was using a class to hold all the modified and created data. My solution was based upon this:

Another solution

However now I have been told I cannot use a class as is used in this solution where the data is held in a class RowInfo. I must use fields directly as in the classes Test1 and Test2 I show above.

Can anyone suggest how I could arrange for the HTML code to be reused by more than one class. I understand I could use a few different ways but all that I know of seem to not allow me to share code between multiple classes that share the same fields.

I hope this makes sense. If not tell me and I will explain more.

  • 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-26T21:19:23+00:00Added an answer on May 26, 2026 at 9:19 pm

    You could define a base view model:

    public abstract class BaseViewModel
    {
        [DisplayName("Created By")]
        public virtual string CreatedBy { get; set; }
        [DisplayName("Modified By")]
        public virtual string ModifiedBy { get; set; }
    }
    

    and then have the two similar view models derive from this base view model (as they are sharing common functionality):

    public class Test1 : BaseViewModel 
    {
    }
    
    public class Test2 : BaseViewModel 
    {
    }
    

    Next you could define a custom editor template for the base view model (~/Views/Shared/EditorTemplates/BaseViewModel.cshtml):

    @model BaseViewModel
    <div class="a">
        @Html.LabelFor(model => model.CreatedBy)
        @Html.TextBoxFor(model => model.CreatedBy)
    </div>
    <div class="b">
        @Html.LabelFor(model => model.ModifiedBy)
        @Html.TextBoxFor(model => model.ModifiedBy)
    </div>
    

    As a side note you could also use an interface instead of an abstract class for the base model:

    public interface IBaseViewModel
    {
        [DisplayName("Created By")]
        string CreatedBy { get; set; }
        [DisplayName("Modified By")]
        string ModifiedBy { get; set; }
    }
    

    and have the editor template typed typed to this interface.

    Then for your main view model:

    public class MyViewModel
    {
        public Test1 Test1 { get; set; }
        public Test2 Test2 { get; set; }
    }
    

    you could invoke the custom editor template using the EditorFor helper:

    @model MyViewModel
    @Html.EditorFor(x => x.Test1)
    @Html.EditorFor(x => x.Test2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an hierarchy of classes that looks like the following: class Critical {
I have got the following class structure, and have plenty of classes like C
I have some classes layed out like this class A { public virtual void
I have trouble when designing classes like this class C1 { public: void foo();
I have classes which have automatic properties only like public customerName {get; set;}. They
I have two classes defined as such: public class Questionnaire { public int QuestionnaireID
We have a project structure like following: sharedlib (lib-project, containing classes, that are useful
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have a package with logic classes(like CheckAuthenticationDataLogic.java, GetVocabulariesLogic.java). And another class - ApiService.java

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.