How to display encoded HTML as decoded in MVC 3 Razor?

后端 未结 7 1469
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 21:54

I\'m using Razor in MVC 3 and Asp.net C#.

I have a View with the following code. model.ContentBody has some HTML tags.

I would need display this HTM

相关标签:
7条回答
  • 2021-02-06 22:35

    @Html.Raw was not work for me here is example:-

      string name = "<p><span style="font-size: small; color: #ff0000;"><span style="font-size: small;"> <span style="font-size: large; color: #000000;">Hi</span><br />  <br />This is just a sample,<br />This will not work with @Html.Raw(),<br />";
      <span>@Html.Raw(name);</span>
    

    But this worked instead:-

    @MvcHtmlString.Create(HttpUtility.HtmlDecode(@model.ContentBody))
    

    or you can also use :-

    @Html.Raw(HttpUtility.HtmlDecode(@model.ContentBody));
    
    0 讨论(0)
  • 2021-02-06 22:39

    You could also decode html in the controller before sending the model to the view,

    WebUtility.HtmlDecode()

        public ActionResult Index(int id)
        {
            var content = _contentRepository.GetContent(id);
            var classViewModel = new ClassViewModel
                                     {
                                         ContentBody = WebUtility.HtmlDecode(ClassViewModel.ContentBody)
                                     };
            return View(classViewModel);
        }
    
    0 讨论(0)
  • 2021-02-06 22:42

    Use this code in the controller:

    string noHTML = Regex.Replace(inputHTML, @"<[^>]+>|&nbsp;", "").Trim();
    
    0 讨论(0)
  • 2021-02-06 22:54
    <div class="display-field">
        @Html.Raw(Model.ContentBody)
    </div>
    

    This code solved the problem!

    0 讨论(0)
  • 2021-02-06 22:57

    Well, to summarize.. In my service/Controller, while returning the model to the view

        ....
        Description = WebUtility.HtmlDecode(narration.Narration1)
    

    My cshtml, where the tinyMCE is displayed.. just bind regularly (I was using HtmlAttributeHelper for other purpose. You can ignore it)

     <div>
        @Html.TextAreaFor(model => model.Description, HtmlAttributeHelper.ConditionalDisable(false, new {@class = "tinymce"}))
     </div>
    

    And in the cshtml page where the data is displayed..

     ......
     <td class="col-word-wrap">@Html.Raw(HttpUtility.HtmlDecode(@item.Narration1))</td>
    
    0 讨论(0)
  • 2021-02-06 22:57

    Please use to display Html use with decode.

    @MvcHtmlString.Create(@Model.OurVision)   
    
    0 讨论(0)
提交回复
热议问题