Using FCK Editor with ASP.NET MVC

First of, what is this FCKEditor? Well, it’s an HTML Text Editor. It’s free and open source, cross browser, extensible and there are probably quite a few more buzzwords applicable. Oh… and it’s also the default HTML Text Editor provided with DotNetNuke. Fck Editor is soon to be replaced by CK Editor but I’ll stick with Fck for now. CK will work mostly the same though.image

First, you’ll need to download Fck Editor. Unzip the downloaded file to see what’s in there.

You’ll need to copy the “editor” folder over to you Contents folder in you ASP.NET MVC website. I chose the path “/Content/Js/ Fck” to paste it in.

Two other files you might want to copy over to this same folder are in the root of the downoaded zipfile: fckeditor.js and fckconfig.js. The latter is expected by the editor and (suprisingly) contains configuration settings for the editor, while the first contains a few nice javascript functions to instanciate the editor.


Next, include the fckeditor.js file in your page:

<script src="<%= Url.Content("~/Content/Js/fck/fckeditor.js") %>" type="text/javascript" ></script>

All set. Now you’ll need to add a textarea to your page:

<%= Html.TextArea("FckEditor1", "Some Value", 
new { @name="FckEditor1" })%>

Important thing here, is the name property of the TextArea being set to the same value as the Id of it.

To finish the client side of things off, you’l need to add some javascript to turn the textarea into a FckEditor at runtime. This can be copied from the samples of course, except for the Url.Contents part:

<script type="text/javascript">
window.onload = function()
var sBasePath = '<%= Url.Content("~/Content/Js/Fck/") %>';

var oFCKeditor = new FCKeditor( 'FckEditor1' ) ;
oFCKeditor.BasePath = sBasePath ;
oFCKeditor.ReplaceTextarea() ;

Now, to prevent the server error “A potentially dangerous Request.Form value was detected from the client “, you should add the attribute “ValidateInput” to your controller’s action method like this”:

public ActionResult Save(int id)

With this, you’re all set. The contents of the TextArea field you started out with, will contain the contents of the FckEditor control.

Have fun!

5 thoughts on “Using FCK Editor with ASP.NET MVC”

  1. I think this should do, but feel free to leave a comment if it doesn’t. I can always make things more clear where useful, right?

  2. I just tried your code, and everthing worked for me except that I had to use a closing tag to include fckeditor.js:

  3. Actually, there was an error in it, pointed out by Christian Donoso, which I corrected only just now:

    needs the closing tag…

    Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *