Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Add double quotes inside a string


For if Model.Class is not null, add the string value for Model.Class inside a class="" - I'm trying to output this: class="class1 class2"

The problem is that I want to add quotes in case there are multiple class names. EpiServer isn't letting me do this. Here's what I've tried...

  1. @(Model.Class != null ? @"class=""" + Model.Class + @"""" : "")
    Renders class="class1 class2"

  2. @(Model.Class != null ? "class='" + Model.Class + "'" : "")
    Renders the single quote as "

  3. @(Model.Class != null ? "class=\"" + Model.Class + "\"" : "")
    Renders the same as #1 above.

Is this possibly an issue with my local version of Epi? I'm not a programmer and I don't know how Epi was set up for my company. Is there something else I should try? Thank you!

Jan 27, 2017 16:49

Hi Nathan,

First, I would take the logic out of the view. Then on the page type with the classes, I would add something like this (Class1 & Class2 are string properties on your model):

public virtual string CssClass => $"{Class1}{Class2}";

Then in your view, you would just do: <h1 class="@Model.CurrentPage.CssClass">Episerver Rules!</h1>. The cool thing about this is you can add as many string as you want to CssClass and it will only render the ones that are not null. If they are all null, it simply adds <h1 class></h1>. I hope this helps!


Edited, Jan 28, 2017 16:32

Avoid adding properties to ContentType for computed properties (even with ignore) Use custom viewmodel for that instead or you risk getting some very nasty bugs. That object is cached in background so it's basically a global static variable in that case.

Jan 28, 2017 20:34

Thanks for the info Daniel! After reading up a bit on the perils of global static variables, it makes total sense. So, here is my refactored approach, let me know what you think: 

Lets say the individual class strings are on my page type NewsPage. Here is my custom viewmodel:

public class NewsPageViewModel : IPageViewModel<NewsPage>
        public NewsPage CurrentPage { get; set; }
        public IEnumerable<PostedComment> CommentList { get; set; }
        public bool HasCommentPublishAccess { get; set; }
        public bool CommentFolderIsSet { get; set; }
        public string CssClass { get; set; }

        public NewsPageViewModel(NewsPage currentPage)
            CurrentPage = currentPage;
            CssClass = GetCssClass(currentPage);

        private string GetCssClass(NewsPage page)
            return $"{page.Class1}{page.Class2}";

Now, in my NewsPage view, which is using NewsPageViewModel as the model, I can do this:
<h1 class="@Model.CssClass">Episerver Rules!</h1>.


Jan 29, 2017 2:44

Looks great!

Jan 29, 2017 7:39
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.