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

  • Home
  • SEARCH
  • 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 4344822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:57:03+00:00 2026-05-21T11:57:03+00:00

I have two views that render a table on each. Most of the tables

  • 0

I have two views that render a table on each. Most of the tables are shared between both views so I want to be able to reuse the view code for them both.

Table 1

Column A   |   Column B
1          |   2
4          |   8

Table 2

Column A   |   Column B   |   Column C
1          |   2          |   7
4          |   8          |   2

Obviously, my actual tables are a lot more complicated than the example above, which only serves to compound the problem. Because of how tables are written in HTML, there seems to me no good way to cut out the stuff I want to reuse while still having the ability to add to it.

Using tables, are there any methods I can employ to reuse the code or will I have to move away from using tables?

  • 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-21T11:57:04+00:00Added an answer on May 21, 2026 at 11:57 am

    Why not just create a view that specializes in building a table given a specific model that includes all the column and row information? That way, you can use the view anywhere you want, and the model you give it will determine how many columns and rows there are.

    Edit

    I don’t think you’re quite understanding my point, so I’ll use some code. Right now you probably have a model that looks something like this:

    public class Something
    {
        public string ValueA {get;set;}
        public string ValueB {get;set;}
        public string ValueC {get;set;}
    }
    

    And your view probably looks something like this:

    <table><tr><th>Column A</th><th>Column B</th><th>Column C</th></tr>
        <%foreach(var something in Model.Somethings) {%>
           <tr>
               <td><%:something.ValueA%></td>
               <td><%:something.ValueB%></td>
               <td><%:something.ValueA%></td>
           </tr>
        <%}%>
    </table>
    

    Well, as you have noticed, this doesn’t lend itself well to simply columns from the table. One solution would be to set some options on the module to determine which columns get included, and then throw a bunch of if/then statements into your view. But I think a better solution would be to make a new Model class that looks more like this:

    public class TableModel
    {
        public IEnumerable<string> Headers {get;set;}
        public IEnumerable<IEnumerable<string>> Rows {get;set;}
    }
    

    Then your model looks more like this:

    <table><tr>
        <%foreach(var header in Model.Headers) {%>
            <th><%:header%></th>
        <%}%></tr>
        <%foreach(var row in Model.Rows) {%>
           <tr>
           <%foreach(var cell in row) {%>
               <td><%:cell%></td>
            <%}%>
           </tr>
        <%}%>
    </table>
    

    As you can see, there is nothing in this view code that ties itself explicitly to your original data type, so it can be reused for any table you want to create throughout your entire application. Your controller becomes responsible for creating a TableModel object from the data that you want to represent. If a particular column gets included in that model, it will be displayed. If not, it won’t.

    Obviously, like your example, this is extremely simplified, but it should give you the general idea. The DataTable class, which is already part of .NET, might already have everything you need, so you won’t even have to create your own TableModel class. It’s already got some nice functionality around adding and removing columns, rows, etc.

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

Sidebar

Related Questions

I have a Delphi 7 application that has two views of a document (e.g.
I have a view that is joining two tables and ordering by the first
I have two applications written in Java that communicate with each other using XML
I have two identical tables and need to copy rows from table to another.
I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two classes, and want to include a static instance of one class
I have two tables set up in a many-to-many relationship: Incidents and Users. When
I have a layout which it has two partial views, one for LogIn and
I have two threads in an Android application, one is the view thread, and

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.