ASP.NET MVC 5 - Rich Text (WYSIWYG) Editor

An interactive website requires interactive components in it for target audience to interact in a more user-friendly way. Getting long rich content kind of text from the target audience adds a responsibility towards the Website to do half of the work for the user e.g. auto spell checker, allowing the user to place inline images or the links, and content indentation etc. These features can be provided individually, but, it will become more cumbersome for the website developers to develop each and every single feature from scratch. In regards to such matter, many amazing affordable rich text editor plugins are available for the commercial use.
 
asp.net mvc5 rich text wysiwyg editor 
 
Today, I shall be demonstrating how to integrate a rich text editor into ASP.NET MVC5 environment. I will be using Summernote Rich Text (WYSIWYG) Editor. This particular plugin is simple to use and the best part I like about it is that it allows the inline images, which so far, I am at least unable to find free of cost, simple to use and with working inline image media in different plugins.
 
The prerequisites include the knowledge about the technologies given below.
  1. ASP.NET MVC 5
  2. HTML
  3. JavaScript
  4. Bootstrap
  5. jQuery
  6. C# Programming
The running working solution source code is being developed in Microsoft Visual Studio 2015 Enterprise.
 
Now, let's begin.
  1. Create new MVC Web project, name it "RichTextEditor".
  2. In "Model" folder, create "RichTextEditorViewModel.cs" file and place the code given below in it i.e.
    ...
        public class RichTextEditorViewModel {
        [AllowHtml]
        [Display(Name = "Message")]
        public string Message {
            get;
            set;
        }
     ... 

    In the code given above, I have simply created a simple model with the message property only. This variable will contain all the text that the user will write inside the rich text editor. The important part here is the "[AllowHtml]" attribute. This will allow the message variable save all HTML generated content by the rich text editor, which includes any formatting or media content. The important point to be noted here is that we cannot use "[Require]" attribute here with rich text editor. We can however, achieve it easily via code, when posting the content to controller, although I am not going to show it here

  3. Create ''Home->Index.cshtml" file and replace it with the code given below i.e.
    @using RichTextEditor.Models  
    @model RichTextEditorViewModel  
    @ {  
        ViewBag.Title = "ASP.NET MVC5: Rich Text Editor Plugin";  
    } < h2 > @ViewBag.Title. < /h2> < div class = "row" > < div class = "col-md-12" > < section id = "loginForm" > @using(Html.BeginForm("Index", "Home", new {  
        ReturnUrl = ViewBag.ReturnUrl  
    }, FormMethod.Post, new {  
        @class = "form-horizontal", role = "form"  
    })) {  
        @Html.AntiForgeryToken() < h5 > < i class = "fa fa-link"  
        aria - hidden = "true" > < /i> < a href = "http://summernote.org/" > Summernote Rich Text WYSIWYG Editor < /a> < /h5> < hr / > < div class = "form-group" > < label class = "col-md-4 control-label" > Message < /label> < div class = "col-md-8" > < div class = "input-group" > @Html.TextAreaFor(m => m.Message, new {  
            rows = "20", style = "resize:none;width:400px;", placeholder = Html.DisplayNameFor(m => m.Message), @class = "form-control input-lg textarea-editor"  
        }) < /div> < /div> < /div>  
        @ * < div class = "form-group" > < div class = "col-md-offset-2 col-md-10" > < input type = "submit"  
        value = "Log in"  
        class = "btn btn-default" / > < /div> < /div>*@  
    } < /section> < /div> < /div>  
    @section Scripts { < script type = "text/javascript"  
        src = "http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.js" > < /script>  
        @Scripts.Render("~/bundles/Script-custom-editor") < link href = "http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.css"  
        rel = "stylesheet" >  
    }  

    In the code given above, I have created my textarea control and apply the Summernote Rich Text (WYSIWYG) Editor plugin. I have also included in this page; the reference JavaScript and CSS style files for this plugin.

  4. In "Scripts" folder, create a new file and name it "script-custom-editor.js". Place the code given below into this JavaScript file.
    $(document).ready(function() {  
        // Initialize Editor  
        $('.textarea-editor').summernote({  
            height: 300, // set editor height  
            minHeight: null, // set minimum height of editor  
            maxHeight: null, // set maximum height of editor  
            focus: true // set focus to editable area after initializing summernote  
        });  
    });  

    In the code given above, I have attached the Summernote Rich Text (WYSIWYG) Editor plugin with my textarea HTML control, which will capture the rich text from the end-user. I have used basic settings for the plugin.

  5. Now, execute the project.

Conclusion

 
In this article, you have learned about the importance, usage, and advantages of rich text editor. You also learned about using Summernote Rich Text (WYSIWYG) Editor plugin with ASP.NET MVC5.


Similar Articles