javascript - 从 TinyMCE 编辑器获取内容

标签 javascript asp.net-mvc-4 tinymce

我试图从 TinyMCE 获取内容,但它只返回空值。它在对话框中加载的问题。 对话框 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>

打开对话框的 View :

<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>

Controller :

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


  AllowGet);
}

附言I have used this example to create the modal dialog .这是一个 PartialView。可以在主索引 View 中从 tinymce 获取内容。但不是在 ajax 调用中。

最佳答案

这个问题的解决方案相当简单。原因是 tinymce 正在返回 html 文本,这在默认情况下是不允许的。解决方案是将 [ValidateInput(false)] 放在 Controller 方法上。

关于javascript - 从 TinyMCE 编辑器获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14338809/

相关文章:

c# - 从 ASP.NET 日历控件的单元格触发事件

javascript - 为原型(prototype)分配对象文字与函数之间的区别

asp.net-mvc - MVC & Elmah - Elmah 可以使用 MVC 站点布局显示吗?

javascript - 将鼠标悬停在图像上以显示由 alt 标签链接的更大的隐藏图像?

c# - 如何先创建 EntityFramework 代码的可视化模型

jquery - MVC4 Ajax 在 Windows 滚动上加载记录

javascript - 调整高度tinyMCE编辑器

javascript - 如何检测 tinyMCE textarea blur/focusout 事件?

javascript - tinyMCE 在外面点击,textarea 隐藏

javascript - 从 Javascript 变量中的数据创建 HTML 图像