简体   繁体   English

从TinyMCE编辑器获取内容

[英]Get content from TinyMCE editor

I'm trying to get the Content from TinyMCE, but it only returns null. 我正在尝试从TinyMCE获取内容,但它仅返回null。 The problem it's loaded in a Dialog box. 问题已加载到对话框中。 The dialog view: 对话框视图:

<form>
  <textarea name="content" cols="40" rows="25" id="tinymce"> 
    Dette er noget tekst
        </textarea>
</form>

<input class="close" onclick="get_editor_content()" name="submit" type="submit" value="Kontakt Oline" style="float: right" id="contenttiny" />
<script type="text/javascript">
  tinyMCE.init({
    // General options
    mode: "textareas",
    theme: "advanced",
    plugins: "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
</script>

The view where the dialog is opened from: 从以下位置打开对话框的视图:

<a class="openDialog" data-dialog-id="emailDialog" data-dialog-title="Kontakt/prospekt" href="/Home/tinymce">Contact US</a>

<div id="result"></div>

<script type="text/javascript">
  $.ajaxSetup({ cache: false });
  $(document).ready(function () {

    $(".openDialog").live("click", function (e) {
      e.preventDefault();
      $("<div ></div>")
        .addClass("dialog")
        .attr("id", $(this).attr("data-dialog-id"))
        .appendTo("body")
        .dialog({
          title: $(this).attr("data-dialog-title"),
          close: function () { $(this).remove() },
          modal: true,
          position: ['center', 40],
          minWidth: 670,
          resizable: false
        })
        .load(this.href);
    });
  });


  $(".close").live("click", function (e) {
    e.preventDefault();

    var content = tinyMCE.get('tinymce').getContent(); //$("#contenttiny").val();
    $.ajax({
      type: "POST",
      url: "/Home/tinymce",

      data: { "content": content },

      success: function (data) {

        $("#result").html(data.nameret);
        $(".dialog").dialog("close");
      },

      error: function (data) {
        alert("There was error processing this");
        $(this).closest(".dialog").dialog("close");
      }
    });

  });
</script>

The Controller: 控制器:

[HttpPost]
public ActionResult tinymce(string content)
 {    /*Your other processing logic will go here*/
  return Json(new
  {
    nameret = content
  }, JsonRequestBehavior.


  AllowGet);
}

PS I have used this example to create the modal dialog . PS 我已使用此示例创建模式对话框 Which is an PartialView. 这是PartialView。 It's possible to get the content from tinymce in the main index view. 可以在主索引视图中从tinymce获取内容。 But not in the ajax call. 但不在ajax调用中。

The solution to the problem was rather simple. 解决问题的方法非常简单。 The reason was that the tinymce is returning html text, which per default is not allowed. 原因是tinymce返回的是html文本,默认情况下不允许这样做。 The solution was to put [ValidateInput(false)] over the Controller method. 解决方案是将[ValidateInput(false)]放在Controller方法上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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