2

Not sure how if it is possible, but I have this in a class:

public string TextNotIncluded 
{ 
    get
    { 
        return ("which is <u>not</u> included in the Quote");
    }
}

The <u> and </u> are being displayed in my view, rather than the word not being underlined. I am not familiar with C#.

Can anyone provide a quick answer?

Thanks.

Edit:

I am just calling this in my view thusly: @MyClass.TextNotIncluded. Wrapping it with @Html.Raw is not efficient in my case because I have this sprinkled throughout dozens of views.

2
  • It depends what control you're outputting that string in. I'd guess that whatever you're using does not parse HTML markup. Commented Apr 15, 2013 at 20:38
  • 1
    show us how you are consuming this property from your MVC view or controller Commented Apr 15, 2013 at 20:39

5 Answers 5

10

There's nothing fundamentally wrong with doing that but it probably won't render the way you're expecting.

You can use @Html.Raw as others have suggested, but I think it's better to explicitly declare your model in such a way as to indicate that it may contain html. You probably want to use the MvcHtmlString class for this instead:

public MvcHtmlString TextNotIncluded 
{ 
    get { return MvcHtmlString.Create("which is <u>not</u> included in the Quote"); }
}

Then in your view you can just use:

@Model.TextNotIncluded
Sign up to request clarification or add additional context in comments.

4 Comments

+1 For altering the property's return type, since it is returning html anyway I would vote for a change at the source too.
As a side-note, this assumes that your model is specific to your MVC project - if your model lives in a DTO or data layer, it wouldn't make sense for a property to be an MvcHtmlString.
Thanks. Also, thanks to all the responses. @p.s.w.g's answer was most complete in that it gave sound reasoning for declaring in my model (plus, if I use this 100 times, it's technically less code/characters). +1 to all the other good suggestions.
@JoeEnos true, but FWIW, you probably want to separate your view models from your data models anyway.
7

If you're using Razor, strings are HTML-encoded by default - you'll need to use Html.Raw to turn off the encoding:

@Html.Raw(x.TextNotIncluded)

In the ASPX engine, you would use <%= %>

<%= x.TextNotIncluded %> - this gives you the raw text
<%: x.TextNotIncluded %> - this HTML-encodes your text - you don't want this.

Comments

4

To output raw HTML, use the Raw HTML helper:

@Html.Raw(TextNotIncluded)

This helper doesn't HTML encode the input, so be careful when using it.

1 Comment

+1 on the "be careful" part - raw HTML opens you up to possible attacks if the text is user-generated.
1

You need to HTML encode the string. Most are recommending the MVC approach, but I would make it more independent of the presentation layer.

public string TextNotIncluded { 
    get { 
        return System.Web.HttpUtility.HtmlEncode("which is <u>not</u> included in the Quote"); 
    }
}

1 Comment

This is backward. This would make the property's value which is &lt;u&gt;not&lt;/u&gt; included, so if you tried to put that on the page, you'd either see it double-encoded or as plain text, but you'd never see it was underlined text.
1

You can use either

@Html.Raw(Model.TextNotIncluded)

or

@MvcHtmlString.Create(Model.TextNotIncluded)

in your view.

But it would be better to alter the return type of the property:

public MvcHtmlString TextNotIncluded
{
    get
    {
        return MvcHtmlString.Create("which is <u>not</u> included in the Quote");
    }
}

1 Comment

Thanks. Same answer as @p.s.w.g basically, but his came first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.