简体   繁体   中英

Displaying text with '<b>' tags as html

How do I display my string that is: <b>test</b> as: test in my html page? I'm getting the string from my database, which was created with a wysiwyg editor.

I've tried using razor syntax:

@Html.Encode(Model.teststring);

But this doesn't work.

I've also tried using javascript, which didn't work either:

<div id="@Model.ID">
   <script type="text/javascript">
      $('#@Model.ID').html(Model.teststring);
   </script>
</div>

Both show the text as follows: <b>test</b>

Is there a way to fix my problem?

You need to place it within the Html.Raw() like below

@Html.Raw(Model.teststring)

This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML. You can find out more about it here

There are two problems in your ID-naming:

  1. Don't use the @-character and
  2. Don't use the dot-character (.)

This seems to work well:

 var Model = { teststring: '<b>test</b>' }; $('#ModelID').html(Model.teststring); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div id="ModelID"> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM