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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:10:44+00:00 2026-05-30T05:10:44+00:00

I have a model with some inheritance worked into it, but here’s the thing,

  • 0

I have a model with some inheritance worked into it, but here’s the thing, I want to apply different HTML attributes depending on which type it is. So my thought was to create a property, I’ll call it DisplayString, which is used as the first argument to String.Format. DisplayString is used to allow me to do something like this:

Classes:

public class Reaction
{
    public string ReactionString { get; set; }
    public string DisplayString { get; set; }

    public Reaction(string reactionString)
    {
        ReactionString = reactionString;
        DisplayString = "It was {0}!"
    }

    public Reaction(string reactionString, string displayString)
    {
        ReactionString = reactionString;
        DisplayString = displayString;
    }

}

public class GoodReaction : Reaction
{
    public GoodReaction()
    {
        base("awesome");
    }
}

public class BadReaction : Reaction
{
    public GoodReaction()
    {
        base("horrible");
    }
}

public class AverageReaction : Reaction
{
    public GoodReaction()
    {
        base("alright", "It kinda was {0}...");
    }
}

View:

@model Reaction

@String.Format(Model.DisplayString, "<strong>" + Model.ReactionString + "</strong>");

So basically every subclass has a string I want to display, but I have varying text AND varying markup. Of course doing it this way just results with the strong tags rendered as text. So what are my options? Feel free to ask any questions needed to clarify, hopefully I got my question across properly. Thanks!


EDIT:

So I wrapped it in Html.Raw() and that did the trick, although I’m sure there is something bad about doing that and I should probably encode each piece, then add tags, then raw, or something, but for my purposes it works.

  • 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-30T05:10:46+00:00Added an answer on May 30, 2026 at 5:10 am

    I would create a display template for each concrete class, and MVC will be smart enough to pick the right one.

    E.g when you call @Html.DisplayFor(model => model.Reaction), where Reaction is declared as the base class, MVC will work out what type it is, then look for a template matching that type, if it doesn’t find it, it will look for a template of the base type, then finally fallback to the default template. I do this a lot in my application.

    Sounds like you should also make the Reaction class abstract, and the ctor’s protected.

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

Sidebar

Related Questions

We have an entity framework model with has some inheritance in it. The following
I have a Project model and it has some text attributes, one is summary.
I have some model objects I'm using in my Java client application. Later these
I have model Article it has field title with some text that may contain
I have a class that contains data from some model. This class has metadata
I have a basic model in which i have specified some of the fields
I have a Python project with following directory structure: /(some files) /model/(python files) /tools/(more
I have some configuration data that I'd like to model in code as so:
I have some constants that represent the valid options in one of my model's
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,

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.