1

I want to set a html content to TinyMCE editor content in ASP.NET MVC

so I come come with a solution that convert that HTML file to string in server side and then call it using ajax in client side

this is C# controller method

[HttpGet]
public string TyneMice()
{
    return System.IO.File.ReadAllText(@"C:\Users\..\myhtml.html");
}

this is ajax call ,

<script type="text/javascript">    

          tinymce.init({

            ...,

            setup: function (ed) {
                ed.on("init", function (ed) {

                    $.ajax({
                        type: "GET",
                        url: "Brochure/TyneMice",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {

                            tinyMCE.activeEditor.setContent(data);
                        },

                        error: function () { alert("Ajax Error"); }
                    });

                })
            }

        });

 </script>

But Once I put debug point on above string method it is not invoking

Then I put above script inside $( document ).ready(function() { ... }

But the same result it doesnt work.

kez
  • 2,273
  • 9
  • 64
  • 123
  • try replacing dataType with false and we rather than using replace string with IActionResult in vnext and ActionResult in other version and return Content – blin2linkme Oct 28 '15 at 06:50
  • 1
    What errors are you getting? Your url should be `url: @Url.Action("TyneMice", "Brochure")',` and your returning a `string`, not json so either use `return Json(...)` or use `dataType: "text",` –  Oct 28 '15 at 06:50
  • @StephenMuecke I've changed as you said but no errors occurring while compile time or run time , still that debug point not invoking – kez Oct 28 '15 at 07:09
  • @blin2linkme you meant like this `dataType: false,` and `public string IActionResult() {` – kez Oct 28 '15 at 07:12
  • `tinymce.init({` then `ed.on("init", function (ed) {` does not look right. Try commenting out the line `ed.on("init", function (ed) {` and its closing `});` –  Oct 28 '15 at 07:15
  • @StephenMuecke I tried like this as you mentioned `tinymce.init({ ....,setup: function (data) {$.ajax({ type: "GET", url:'@Url.Action("TyneMice", "Brochure")',contentType: "application/json; charset=utf-8", dataType: "text", success: function (data) { tinyMCE.activeEditor.setContent(data); }, error: function () { alert("Ajax Error"); } }); } });` but seems like then tinymce editor missing once I do that – kez Oct 28 '15 at 07:25
  • Looking at the docs, `setup` is only for attaching events. I think what you want is [setContent](http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.setContent) (but I'm not familiar with TinyMCE so not sure) –  Oct 28 '15 at 07:30
  • if above Ajax call success I've sets the HTML contents of the activeEditor editor `tinyMCE.activeEditor.setContent(data);` so Im in right path noh ??? – kez Oct 28 '15 at 07:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93569/discussion-between-stephen-muecke-and-kez). –  Oct 28 '15 at 07:58
  • @StephenMuecke once I use in this way `var content; $.ajax({type: "GET", url: '@Url.Action("TyneMice", "Brochure")', ....., success: function (data) { content = data; }, error: function () { alert("Ajax Error"); } }), tinymce.init({ ............., setup: function (ed) { tinyMCE.activeEditor.setContent(content, { format: 'html' }); } });` this is how it look like , TinyMCE editor not loading http://s22.postimg.org/68exg1rjl/erer.jpg – kez Oct 28 '15 at 09:32
  • @StephenMuecke is this TyneMice() method should be inside the viewpage or codebehind one( .cshtml file or .aspx file)? like this question http://stackoverflow.com/questions/4508409/ajax-method-call/4508430#4508430 – kez Oct 28 '15 at 09:37
  • Not sure what your saying. There is no 'code behind' in MVC (and MVC can have either `.cshtml` (razor) of `.aspx` files. –  Oct 28 '15 at 09:43
  • @StephenMuecke in this question answer http://stackoverflow.com/questions/4508409/ajax-method-call/4508430#4508430 he put that string method code inside .aspx page , shall I follow like that , is it good approch – kez Oct 28 '15 at 09:45
  • That only because its a very old answer (before razor) It makes no difference. Stick with your `.cshtml` files –  Oct 28 '15 at 09:48
  • @StephenMuecke can I add string method code inside .cshtml file ? – kez Oct 28 '15 at 09:50

1 Answers1

0

Your Action should return of type ActionResult. The action will return Json object with JsonRequestBehavior set as AllowGet.

[HttpGet]
public JsonResult TyneMice()
{           
    return Json("TyneMice Example", JsonRequestBehavior.AllowGet);
}
user1672994
  • 10,509
  • 1
  • 19
  • 32