Bootstrap WYSIWYG Editor in ASP.Net MVC

Introduction

This article introduces how to implement a Bootstrap WYSIWYG editor in an ASP.NET MVC application. There are many editors available over the internet but here we will implement the Summer note editor that is a simple WYSIWYG editor using Bootstrap. Summer note is a JavaScript library that helps us to create a WYSIWYG editor. You can download it from either GitHub or the official site.

Using the Code

Summer note uses the open-source libraries jQuery, Bootstrap and Font Awesome. To implement this in an ASP.NET MVC application we will create an MVC project in Visual Studio. First of all we will create bundles required for both JavaScript and CSS files. The following code snippet shows the BundleConfig class.

  1. using System.Web.Optimization;  
  2.   
  3. namespace BootstrapEditor.App_Start  
  4. {  
  5.     public class BundleConfig  
  6.     {  
  7.         public static void RegisterBundles(BundleCollection bundles)  
  8.         {  
  9.             bundles.Add(new StyleBundle("~/Content/css").Include(  
  10.                      "~/Content/css/bootstrap.css",  
  11.                      "~/Content/css/font-awesome.css",  
  12.                      "~/Content/css/site.css"  
  13.                    ));  
  14.   
  15.             bundles.Add(new StyleBundle("~/Content/summernote").Include(  
  16.                 "~/Content/summernote/summernote.css"  
  17.                 ));  
  18.   
  19.             bundles.Add(new ScriptBundle("~/bundle/base").Include(  
  20.                "~/Scripts/jquery-1.10.2.js",  
  21.                 "~/Scripts/bootstrap.js"  
  22.                ));  
  23.   
  24.             bundles.Add(new ScriptBundle("~/bundle/summernote").Include(  
  25.                "~/Content/summernote/summernote.js"  
  26.                ));  
  27.         }  
  28.     }  
  29. }  
Thereafter we will create a view model (HomeViewModel) that will be bind to the view. The following code snippet shows the HomeViewModel class.
  1. namespace BootstrapEditor.Models  
  2. {  
  3.     public class HomeViewModel  
  4.     {  
  5.         public string Title { getset; }  
  6.         public string Content { getset; }  
  7.     }  
  8. }  
Thereafter we will create a controller that handles GET and POST requests for a view. The following code snippet shows HomeController.
  1. using BootstrapEditor.Models;  
  2. using System.Web.Mvc;  
  3.   
  4. namespace BootstrapEditor.Controllers  
  5. {  
  6.     public class HomeController : Controller  
  7.     {  
  8.         [HttpGet]  
  9.         public ActionResult Index()  
  10.         {  
  11.             HomeViewModel model = new HomeViewModel();  
  12.             return View();  
  13.         }  
  14.   
  15.         [HttpPost]  
  16.         public ActionResult Index(HomeViewModel model)  
  17.         {  
  18.             return View(model);  
  19.         }  
  20.     }  
  21. }  
Now we will create a view that renders the editor on UI. The following code snippet shows the index view.
  1. @model BootstrapEditor.Models.HomeViewModel  
  2. @section head{  
  3.     @Styles.Render("~/Content/summernote")  
  4. }  
  5. <div class="panel panel-primary">  
  6.     <div class="panel-heading panel-head">Add Content</div>  
  7.     <div class="panel-body">  
  8.         @using (Html.BeginForm())  
  9.         {  
  10.             <div class="form-horizontal">  
  11.                 <div class="form-group">  
  12.                     @Html.LabelFor(model => model.Title, new { @class = "col-lg-2 control-label" })  
  13.                     <div class="col-lg-9">  
  14.                         @Html.TextBoxFor(model => model.Title, new { @class = "form-control" })  
  15.                     </div>  
  16.                 </div>  
  17.                 <div class="form-group">  
  18.                     @Html.LabelFor(model => model.Content, new { @class = "col-lg-2 control-label" })  
  19.                     <div class="col-lg-9">  
  20.                         @Html.TextAreaFor(model => model.Content, new { @class = "form-control", @row = 5 })  
  21.                     </div>  
  22.                 </div>  
  23.                 <div class="form-group">  
  24.                     <div class="col-lg-9"></div>  
  25.                     <div class="col-lg-3">  
  26.                         <button class="btn btn-success" id="btnSubmit" type="submit">  
  27.                             Submit  
  28.                         </button>  
  29.                     </div>  
  30.                 </div>  
  31.             </div>  
  32.         }  
  33.     </div>  
  34. </div>  
  35. @section scripts{  
  36.     @Scripts.Render("~/bundle/summernote",  
  37.                     "~/Scripts/home-index.js")  
  38. }  
As in the following code snippet, we have a textarea that will be converted to a Summer note text editor. We will use HomeViewModel's Content property for the textarea. That means the content will be its id. Now write a JavaScript file, in that file we will create a text editor on the text area input field. The following code snippet shows that the Summer note text editor renders on the content input field once the web page completes the render.
  1. (function ($) {  
  2.     function HomeIndex() {  
  3.         var $this = this;  
  4.   
  5.         function initialize() {  
  6.             $('#Content').summernote({  
  7.                 focus: true,  
  8.                 height: 150,    
  9.                 codemirror: {   
  10.                     theme: 'united'  
  11.                 }  
  12.             });  
  13.         }  
  14.   
  15.         $this.init = function () {  
  16.             initialize();  
  17.         }  
  18.     }  
  19.     $(function () {  
  20.         var self = new HomeIndex();  
  21.         self.init();  
  22.     })  
  23. }(jQuery))  
The preceding code is defined in the home-index.js file that was already added to the view page. The summernote() method is the key on input field that generates the text editor on any input field. Now run the application and we will get a result as in the following image.

Text Editor on UI
Figure 1: Text Editor on UI

Now we will click on the submit button to submit the form on the server. Here we will get an error such as:
A potentially dangerous Request.Form value was detected from the client (Content="Welcome to<span style="font-we")

This was because .NET detected something in the entered text that looked like an HTML statement. To disable request validation, we will add the [ValidateInput(false)] to the POST action in HomeController as in the following code snippet.
  1. [ValidateInput(false)]  
  2. [HttpPost]  
  3. public ActionResult Index(HomeViewModel model)  
  4. {  
  5.     return View(model);  
  6. }  

 


Similar Articles