0

I am wondering how to get html markup language to be displayed in a web page when using Html Encode which is being used to replace some string like in the example below.

@(Html.Raw(Html.Encode(Model.Test).Replace("\n", "<br />")))

Of course, just using

@(Html.Raw(Model.Name)) e.g.<b>test/b> = test

Will achieve what I am asking for but then I will lose the replace code.

I could do this replacing functionality in the controller which may be the best method. However, I am intrigued to whether this can be done just in the view.

Thanks

2
  • Are you attempting to replace any "\n" entries with "<br />"? If so, you shouldn't need to encode.... Commented Feb 16, 2016 at 15:40
  • Interesting, I will state that isnt the code i wrote, i have picked the code up, so therefore its a working piece of code, so I dont want to change it if necessary and just append it to write Raw html. How else can I replace it? Commented Feb 16, 2016 at 15:53

1 Answer 1

1

You can use

@Html.Raw("<b>test</b>") 

for this.

Html.Encode(Model.Test)

actually changes the string <b> to

&lt;b&gt;

so in fact I think this should be enough

@(Html.Raw(Model.Test.Replace("\n", "<br />")))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I can see why this would work and of course complies correctly, I will test and then mark as correct, but thanks, a pretty simple solution

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.