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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:21:46+00:00 2026-05-18T22:21:46+00:00

This is for .net MVC project i’m working on I have the following classes:

  • 0

This is for .net MVC project i’m working on

I have the following classes:

    public abstract class TemplateBase
{
    public abstract string TemplateName { get; }
    public string RuntimeTypeName { get { return GetType().FullName; } }
}

and 2 classes that inherit from it:

    public class TwoColumnTemplate : TemplateBase
{
    public override string TemplateName { get { return "Two-column page"; } }
    public AreaContainer LeftColumn { get; protected set; }
    public AreaContainer RightColumn { get; protected set; }

    public TwoColumnTemplate()
    {
        LeftColumn = new AreaContainer("Left column");
        RightColumn = new AreaContainer("Right column");
    }
}

    public class SingleColumnTemplate : TemplateBase
{
    public override string TemplateName { get { return "Single-column page"; } }
    public AreaContainer CenterColumn { get; protected set; }

    public SingleColumnTemplate()
    {
        CenterColumn = new AreaContainer("Center column");
    }
}

and a contentpage class that is used for displaying/modifying html content

    public class ContentPage
{
    public virtual int ContentPageId { get; set; }

    public virtual string Title { get; set; }

    public TemplateBase Template { get; set; }
}

Now the question. I’ve created a model binder that binds ContentPage and I do get the data to my ActionResult like so:

        [HttpPost]
    public ActionResult Edit(ContentPage row)
    {
        return this.View(row);
    }

row does have the right data however I can’t access Left or Right column data because abstract class TemplateBase. To clerify is there a way to dynamically cast “row” object to a correct class (SingleColumn or TwoColumn) type?

Instead of this using if/else is there a better way?. Maybe using reflections?
thanks

        [HttpPost]
    public ActionResult Edit(ContentPage row)
    {
        var sb = new StringBuilder();

    if (row.Template.RuntimeTypeName == "TwoColumn")
    {
        TwoColumnTemplate template = (TwoColumnTemplate)row.Template;
    } 
    else
    {
        SingleColumnTemplate template = (SingleColumnTemplate)row.Template;
    }

        return this.View(row);
    }
  • 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-18T22:21:47+00:00Added an answer on May 18, 2026 at 10:21 pm

    You do not need the RuntimeTypeName property at all. The correct way to do type testing would be something like this:

    if (row.Template is TwoColumnTemplate)
    {
        TwoColumnTemplate template = (TwoColumnTemplate)row.Template;
    } 
    else if (row.Template is SingleColumnTemplate)
    {
        SingleColumnTemplate template = (SingleColumnTemplate)row.Template;
    }
    

    However, this screams out for proper OO design. If you have a hierarchy of classes, and you are constantly testing if some object is of some class, your class model is severely broken.

    Perhaps your template classes themselves should be emitting their own HTML or view state objects via an abstract method? The type system gives you all the power you need to solve this problem without testing if an object is of a particular type.

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

Sidebar

Related Questions

ASP.NET MVC - what is this in VB.NET? Html.TextBox( name, null, new { @class
I'm new to ASP.NET MVC so this may be a stupid question. I have
I am trying to learn ASP.NET MVC and I hit this problem: I have
Is it possible to have an ASP .NET MVC 3 project compiled into an
is it possible to create something like this i ASP.NET MVC beta 1 i
ASP.NET MVC has been discussed on this forum a few times. I'm about to
This is somewhat asp.net MVC related only for example purposes but I was hoping
Using asp.net MVC I'd like to do this inside a view: <%= Html.TextBox(textbox1, null,
I know this site is written using ASP.Net MVC and I do not see
In ASP.NET MVC, we're required to use the suffix Controller for all controllers. This

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.