2013-01-15 147 views
2

我试图从TinyMCE获取内容,但它只返回null。它被加载到对话框中的问题。 对话框视图:从TinyMCE编辑器获取内容

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

其中从打开的对话框的视图:

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

控制器:

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


    AllowGet); 
} 

P.S. I have used this example to create the modal dialog。这是一个部分视图。可以在主索引视图中从tinymce获取内容。但不是在ajax调用。

+0

您可以在关闭对话之前获取内容吗? – Thariama

+0

是的。我试图使用这个例子http://stackoverflow.com/questions/14023323/how-to-extract-html-content-from-tinymce-editor?rq=1我得到的内容。但我无法从.close函数中获取它。 – mortenstarck

+0

你可以登录到控制台什么tinyMCE.get('tinymce')包含? – Thariama

回答

0

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