使用ASP.NET MVC 5中的tinyMCE获取和设置html格式

问题描述 投票:0回答:2

我有一个双语项目,每个语言都有一个故事。我想使用TinyMCE来获取格式化的HTML文本,并从数据库中设置这些Textareas。

这是我的博客模型:

模型

    [Required(ErrorMessage = "Please add the news context")]
    [MinLength(10, ErrorMessage = "The length of news context must be 10 characters atleast")]
    [Display(Name = "News Context")]
    [UIHint("tinymce_jguery_full"), AllowHtml]
    public string newsContent { get; set; }

    [Required(ErrorMessage = "شرح خبر نمی تواند خالی باشد")]
    [MinLength(10, ErrorMessage = "حداقل طول شرح خبر 10 حرف است")]
    [Display(Name = "شرح خبر")]
    [UIHint("tinymce_jguery_full"), AllowHtml]
    public string newsContentFa { get; set; }

我也可以使用service.Get_Category_List()方法成功获取该博客,如下NewsmanagementControler中的代码>>

控制器

 public ActionResult Edit(int id)
 {
        using (DbModel db = new DbModel())
        {
            News SelectedNews = new News();
            var news = service.GetNews_ById(id);
            return View(news);
        }
 }

最后在视图中,我从数据库获取的原始HTML中设置了tinyMCE,如下代码:

查看

<script src='//cdn.tinymce.com/4/tinymce.min.js'></script>
<script type="text/javascript">
    tinymce.init({
        selector: 'textarea',
        mode: "textareas",
        theme: 'modern',
        entity_encoding: 'raw',
        height: 300,
        plugins: [
            'advlist autolink link lists charmap print preview hr anchor pagebreak spellchecker',
            'searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime nonbreaking',
            'save table colorpicker contextmenu directionality paste textcolor image'
        ],
        content_css: '../../Content/css/tinyMCE.css',
        toolbar: 'insertfile undo redo | styleselect | fontselect | sizeselect | fontsizeselect bold italic underline | alignleft aligncenter alignright alignjustify | ltr rtl | bullist numlist outdent indent | link | print preview fullpage | forecolor backcolor image',
        fontsize_formats: "8pt 10pt 12pt 14pt 16pt 18pt 20pt 24pt 36pt",
        font_formats: "Arial=arial,helvetica,sans-serif;" +
            "Arial Black=arial black,avant garde;" +
            "B Yekan=BYekan" +
            "Courier New=courier_newregular,courier;" +
            "Impact=impactregular,chicago;" +
            "Tahoma=tahoma,arial,helvetica,sans-serif;" +
            "Times New Roman=times new roman,times;" +
            "Verdana=verdana,geneva;" +
            "Webdings=webdings;" +
            "Wingdings=wingdings,zapf dingbats",
        draggable_modal: true
    });
</script>
@using (Html.BeginForm("Edit", "NewsManagement", FormMethod.Post))
{
        <div class="col-md-12 ltr">
            <br />
            <label for="MainContent">Context of Blog</label>
            @Html.TextAreaFor(m => m.news.newsContent, new { @class = "fullWidth tiny", placeholder = "Main Content" })
            @Html.ValidationMessageFor(m => m.news.newsContent, "", new { @class = "redError" })<br />
            <br />
        </div>
        <div class="col-md-12 rtl">
            <br />
            <label for="MainContentFa">شرح مطلب</label>
            @Html.TextAreaFor(m => m.news.newsContentFa, new { @class = "fullWidth tiny rtl", placeholder = "شرح مطلب" })
            @Html.ValidationMessageFor(m => m.news.newsContentFa, "", new { @class = "redError" })<br />
        </div>
}

我保存原始HTML tinyMCE没有任何问题,但是在数据库中显示我存储的原始html时出现了问题-如此屏幕快照所示:

enter image description here

如您所见,原始HTML不在TinyMCE中呈现。我也在google以及这里的所有位置进行搜索,但是根据我的问题没有正确的答案。

我有一个双语项目,每个语言都有一个故事。我想使用TinyMCE来获取格式化的HTML文本,并从数据库中设置这些Textareas。这是我的博客模型:...

c# asp.net-mvc-5 tinymce-4
2个回答
0
投票

如果您在TinyMCE中看到HTML中的HTML标签,则应用程序的某些部分可能会将HTML的“安全性”转义。大多数现代框架默认情况下都会执行此操作,但是它们都可以在需要时绕过此安全措施。


0
投票

默认情况下,出于安全原因,在MVC中,所有html输入都经过htmlencoded编码,因此,如果要使用html标记作为输入控件的输入,则可以在自己的html中对其进行htmldecode像这样将其绑定到您的html助手之前查看

    @{
      Model.news.newsContent= HttpUtility.HtmlDecode(Model.news.newsContent);
     } 
© www.soinside.com 2019 - 2024. All rights reserved.