What is CKEditor?
CkEditor is a ready-for-use HTML text editor which is designed to simplify web content creation. The best Browser-based rich text editors are usually called WYSIWYG editors. It is very easy to use in any type of project.
In this article, we are going to learn how to implement CKEditor in ASP.NET MVC. In this example, we will cover how to use it, how to post a simple blog or article & how to retrieve the data from the database.
Here, I am going to use just three tables: Technology (used to retrieve the ID of technology), Users (used to retrieve the user's information), and UserPost (used to retrieve the article details).
Step 1. First, I am going to create tables by using the script given below.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Technology](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
NULL,
CONSTRAINT [PK_Technology] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH
(
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH
(
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserPost](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[UserId] [bigint] NOT NULL,
[TechnologyId] [bigint] NOT NULL,
NOT NULL,
NULL,
[Contents] [nvarchar](max) NOT NULL,
[CreationDate] [datetime] NULL,
CONSTRAINT [PK_UserPost] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH
(
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY]
GO
Step 2. Create an empty MVC project by using Visual Studio. Download the latest version of CKEditor from here & add the folder to the Application.


Step 3. I am going to use font-awesome css, bootstrap css, bootstrap JS, and ckeditor.js. Add all the references into a BundleConfig file, as given below.
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/headerJS").Include(
"~/Scripts/External/jquery.min.js",
"~/Scripts/External/bootstrap.min.js",
"~/Scripts/ckeditor/ckeditor.js",
"~/Scripts/ckeditor/samples/js/sample.js"
));
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/css/bootstrap.min.css",
"~/Content/css/custom.css",
"~/Content/css/font-awesome.css",
"~/images/favico.ico",
"~/Content/css/bootstrap-datetimepicker.min.css"
));
bundles.Add(new ScriptBundle("~/bundles/footer").Include(
"~/Scripts/External/jquery.min.js",
"~/Scripts/External/bootstrap.min.js",
"~/Scripts/External/bootstrap-datepicker.js",
"~/Scripts/External/bootstrap-datetimepicker.min.js",
"~/Scripts/Custom/Shared/Layout.js"
));
}
Now, add the reference of the above header, CSS, and footer into a _Layout. cstml.
Step 4. Create a view model ContributeViewModel, which contains some properties, as given below.
public class ContributeViewModel
{
public long UserId { get; set; }
public string TechnologyId { get; set; }
public string Title { get; set; }
public DateTime Date { get; set; }
public string Description { get; set; }
public string Contents { get; set; }
}
Step 5. Now, create controller Contribute, where action method Contribute() will return a View, which will take user inputs like article title, article description, technology name, and Content by using an editor. For technology names, we are using checkboxes.
On the submit button, we are going to save these data into a database by using a repository pattern. Read my article for reference on CRUD operation using repository pattern unit of work.
@model CoderFunda.ViewModel.Contribute.ContributeViewModel
<div class="mainContent">
<div class="col-sm-12">
<span class="pageText1">Contribute your </span><span class="pageText2">Blog</span>
</div>
<div class="col-sm-12">
<div class="col-sm-12 col-md-6">
<dl class="dl-horizontal dlCustom">
<dt>@Html.LabelFor(x => x.Title) :</dt>
<dd>
@Html.TextBoxFor(x => x.Title, new { @class = "form-control", @placeholder = "Enter Title" })
</dd>
<dt>@Html.LabelFor(x => x.TechnologyId, "Techonlogy") :</dt>
<dd>
<div class="checkbox customCheckbox" id="chkTech">
<label>
<input type="checkbox" name="blankCheckbox" id="blankCheckbox" value="Asp.NET">ASP.NET
</label>
<label>
<input type="checkbox" name="blankCheckbox" id="blankCheckbox" value="C#">C#
</label>
<label>
<input type="checkbox" name="blankCheckbox" id="blankCheckbox" value="MVC">MVC
</label>
<label>
<input type="checkbox" name="blankCheckbox" id="blankCheckbox" value="SQL">SQL
</label>
<label>
<input type="checkbox" name="blankCheckbox" id="blankCheckbox" value="Other">Other
</label>
</div>
<label id="lblContributeTechErrorMsg" style="display:none;color:red"></label>
</dd>
</dl>
</div>
<div class="col-sm-12 col-md-6">
<dl class="dl-horizontal dlCustom">
<dt>@Html.LabelFor(x => x.Description) :</dt>
<dd>@Html.TextAreaFor(x => x.Description, new { @class = "form-control", @placeholder = "Description" })</dd>
</dl>
</div>
<div class="col-sm-12">
<dl class="dl-horizontal">
<dt>@Html.LabelFor(x => x.Contents) :</dt>
<dd>
<div id="editor"></div>
<label id="lblContributeContentErrorMsg" style="display:none;color:red"></label>
</dd>
</dl>
<div class="text-center">
<a href="@Url.Action("Home", "Account")" class="customSubmit">BACK</a>
<input type="button" class="customPurple btnSubmit" id="btnSubmit" value="SUBMIT">
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
Now, write the code on the submit button and click event, which will execute an action method using AJAX call in JavaScript, as given below.
$('.btnSubmit').click(function() {
if (ValidateContribute()) {
var Title = $("#Title").val();
var TechnologyId = "";
$('input[name="blankCheckbox"]:checked').each(function() {
if (this.value != '') {
TechnologyId = this.value;
}
});
var Description = $("#Description").val();
var Content = CKEDITOR.instances['editor'].getData();
var ContributeModel = {
Title: Title,
TechnologyId: TechnologyId,
Description: Description,
Contents: Content
};
$.ajax({
url: 'Contribute/Contribute',
type: 'POST',
data: JSON.stringify(ContributeModel),
contentType: 'application/json;charset=utf-8',
success: function(data) {
if (data.success === true) {
window.location.href = "../Account/Home";
} else if (data.success === false) {
alert("Error occurred..!!");
}
},
error: function() {
alert("Error occurred..!!");
}
});
}
});
Step 6. Create an action method in the contribute controller, which will insert the data into a database. If the data is inserted successfully, it will return the JSON result as true; otherwise, it will return false. In this action method, from the GetLoggedInUserId() method, we get the current user ID of the user who is logged into an application.




Fernando UrccovichePosted Sep 18, 2019, 8:31 PM
Missed publish how is the controller, create, how to save the images inserted by the editor?
Ramakant BhoiPosted Oct 11, 2018, 7:04 AM
Do u added link code?
Naresh SinghalPosted Feb 24, 2018, 4:09 AM
Very Nicely Integrated CkEditor....Thanks......its useful.
Paulo CarneiroPosted Jan 7, 2018, 1:01 PM
For some reason my 'editor' div won't appear in the page. I think the id="editor" is sort of a keyword to point where the editor was supposed to show, right?
abhijeet kumarPosted Aug 24, 2017, 7:29 AM
Thanks sir for sharing this article
Naren Kumar KumaresanPosted May 24, 2017, 9:39 AM
Website not working
Dharmraj ThakurPosted May 23, 2017, 12:12 AM
Thanks for share,,,,,,,,,,,,,,,,,,,,,,
Upendra Pratap ShahiPosted May 22, 2017, 1:33 PM
Nice info shared....