65.9K
CodeProject is changing. Read more.
Home

Integrating TinyMCE Editor (WYSIWYG) to an ASP.NET MVC Application

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (3 votes)

May 16, 2022

CPOL

2 min read

viewsIcon

9684

downloadIcon

417

Integrating TinyMCE editor In ASP.NET MVC

Introduction

This is a step by step process of Integrating TinyMCE editor (WYSIWYG) into an ASP.NET MVC application.

Background

There are multiple WYSIWYG editors available which allow users to do text formatting in web applications and keep the output in HTML format. One of them is TinyMCE editor.

Integrating TinyMCE in ASP.NET MVC

  1. Create an ASP.NET MVC Web application.
  2. Choose MVC and click on the OK button.
  3. To integrate TinyMCE, the easiest approach would be to download the latest TinyMCE package via NuGet through the following steps:
  4. Right-click on the project.
  5. Choose Manage NuGet Packages from the Context menu.
  6. Select the Browse tab on the left within the dialogue box.
  7. Search for “TinyMCE” in the upper right-hand corner.
  8. Install the TinyMCE package to your project.
  9. Select TinyMCE latest version to install, by clicking on the Install button, all the necessary folders will be added inside the Scripts folder.
  10. Exclude tinymce.d.ts from the project, if it is there as we do not have a typescript transpiler installed. If we don't exclude it, build might fail due to errors (Example: cannot find name 'InputEvent', etc.). As we are not using typescript to integrate TinyMCE, instead it's been done using JavaScript.
  11. Add a model, TinyMCE.cs is the model created (we can give any name) with a string type property and named it HtmlContent with [AllowHtml] attribute.
    using System.Web.Mvc;
    namespace TinyMCE_Integration_MVC.Models
    {
        public class TinyMCE
        { 
            [AllowHtml]
            public string HtmlContent { get; set; }
        }
    }

    Note: The [AllowHtml] data annotation is used to allow sending HTML content or codes to the server which by default is disabled by ASP.NET MVC to avoid XSS (Cross-Site Scripting) attacks.

  12. Clear the default content from the Index.cshtml page. And add the below code to Index.cshtml.
    @model TinyMCE_Integration_MVC.Models.TinyMCE
    ViewBag.Title = "Home Page";
    <div class="row">
     using (Html.BeginForm())
     Html.TextAreaFor(model => model.HtmlContent)
    < input type="submit" value="Submit" />
    </div>
  13. Add /Scripts/tinymce/tinymce.min.js to BundleConfig.cs.
    bundles.Add(new Bundle("~/bundles/js").Include("~/Scripts/tinymce/tinymce.min.js"));
  14. Refer the bundle in _Layout.cshtml.
    @Scripts.Render("~/bundles/js")
  15. Then, the next step is to add the below script to the <head> section of _Layout.cshtml.
    <script type="text/javascript">
    // Initialize your tinyMCE Editor with your preferred options
    selector: '#HtmlContent' // change this value according to your HTML
    </script>
  16. In HomeController.cs, add one more Index action method with model (TinyMCE.cs type) as to receive the HTML content on Submit button click from Index.cshtml. the Index method with data annotation [HttpPost].
    using System.Web.Mvc;
    using TinyMCE_Integration_MVC.Models;
    namespace TinyMCE_Integration_MVC.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Index(TinyMCE tinyMCEModel)
            {
                return View();
            }
        }
    }
  17. Run the application to see the TinyMCE editor.

Thank you for reading!

History

  • 16th May, 2022: Initial version