- Visual Studio
- SQL Server
- Basic knowledge of ASP.NET MVC
- Basic knowledge of Entity Framework
- Basic knowledge of jQuery
- Basic knowledge of CSS
Article Flow
- Create an ASP.NET MVC Empty project
- Enable Kendo UI features
- Implement Editor
- Save Editor Content to Database using jQuery
- Configure Entity Framework with database and application
- PDF generation
Create an ASP.NET MVC Empty project
To create ASP.NET MVC empty project, follow the below steps one by one. Here, I have used Visual Studio 2017.
- Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it "KendoUI_Editor".
- Now, click OK.
- Then, select Empty MVC template and click "OK" to create the project.
- Once you click OK, the project will be created with the basic architecture of MVC.
- If you are not aware of how to create an Empty ASP.NET Web Application, please visit Step 1 and Step 2 to learn.
Once you complete these steps, you will get the screen as below.
Enable Kendo UI Features
Here, we are going to enable the Kendo UI Editor features with our application by adding the below CSS and JS in the application. Before adding it, let's create the controller and action. Now, create an empty Controller and View. Here, I created a Controller with the name of "EditorController". Whenever we create an empty Controller, it is created with an empty Index action method. And, create an empty View of this action method "Index". Now, add the below Kendo UI JS and CSS in Index or _Layout page.
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.common-material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.mobile.min.css" />
- <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js"></script>
- <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js"></script>
Implement Editor
Create a div in the View which will act as Editor. Here, I have used the text-area and mentioned the id for div in the name of the Editor.
- <div id="example">
- <textarea id="editor"> </textarea>
- </div>
Now, enable the Kendo UI Editor features to div ("editor").
- <script type="text/javascript">
- $(document).ready(function () {
- $("#editor").kendoEditor();
- });
- </script>
Set the default controller and action in RouteConfig.cs.
- public class RouteConfig {
- public static void RegisterRoutes(RouteCollection routes) {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {
- controller = "Home", action = "Index", id = UrlParameter.Optional
- });
- }
- }
Run your application. In the below image, you can see that the basic features of Editors are enabled.
Now, we shall add a few more tools on this Editor to add more features. For that, Kendo provideds the tools property to enable these, as below.
- $("#editor").kendoEditor(
- {
- tools: [
- "bold",
- "italic",
- "underline",
- "strikethrough",
- "justifyLeft",
- "justifyCenter",
- "justifyRight",
- "justifyFull",
- "insertUnorderedList",
- "insertOrderedList",
- "indent",
- "outdent",
- "createLink",
- "unlink",
- "insertImage",
- "insertFile",
- "subscript",
- "superscript",
- "tableWizard",
- "createTable",
- "addRowAbove",
- "addRowBelow",
- "addColumnLeft",
- "addColumnRight",
- "deleteRow",
- "deleteColumn",
- "viewHtml",
- "formatting",
- "cleanFormatting",
- "fontName",
- "fontSize",
- "foreColor",
- "backColor",
- "print",
- ]
- });
Run your application.
Save Editor Content to Database using Jquery
Before formatting the data in Editor, let's create the table in the database to store the contents. I have created the table with the below design in the name of EditorContents.
Execute the below query to get the same design.
- CREATE TABLE [dbo].[EditorContents](
- [ID] [bigint] IDENTITY(1,1) NOT NULL,
- [HtmlContent] [nvarchar](max) NULL,
- CONSTRAINT [PK_EditorContents] 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] TEXTIMAGE_ON [PRIMARY]
- GO
Now, let's format some content using our Kendo Editor, as below.
In the above image, you can see that I have added one Save button. So, add one button in your View as below.
- <input type="button" class="btn btn-primary" id="btnSave" value="Save" />
Configure Entity Framework with database and application
Here, I have already discussed how to configure and implement a database first approach. In the meantime, choose your created table with Entity Framework. Once we finish our configuration with SQL table "EditorContents" from CSharpCorner database and with our application, we will get the below screen as succeeding configuration.
Write the Save Logic in the controller.
- [HttpPost]
- public void SaveHtmlToDatabase(EditorContent editorContent) {
- CSharpCornerEntities cSharpCornerEntities = new CSharpCornerEntities();
- cSharpCornerEntities.EditorContents.Add(editorContent);
- cSharpCornerEntities.SaveChanges();
- }
Now, Pass the Editor value to the above action method using AJAX, as below.
- $("#btnSave").click(function() {
- var content = $("#editor").val();
- var editorContent = new Object();
- editorContent.HtmlContent = content;
- $.ajax({
- type: "POST",
- url: "/Editor/SaveHtmlToDatabase",
- data: editorContent,
- dataType: "json",
- success: function(response) {
- Alert('Success');
- },
- });
- alert(content);
- });
We should define the "Allow HTML" attribute while binding the value to a model property.
- [AllowHtml]
- public string HtmlContent { get; set; }
Run your application. In the below image, you can see that we have stored the HTML content in the database.
PDF Generation
We have formatted the content, right? Let's convert this Editor content to a PDF document. Now, enable the PDF features as below.
- $("#editor").kendoEditor({
- tools: ["pdf"],
- pdf: {
- fileName: "PDFDemoDocument.pdf",
- proxyURL: "https://demos.telerik.com/kendo-ui/service/export",
- paperSize: "a4",
- margin: {
- bottom: 20,
- left: 20,
- right: 20,
- top: 20
- }
- }
- });
Run your application.
Complete View
- @{
- ViewBag.Title = "Editor";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <br />
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.common-material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.min.css" />
- <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2018.3.1017/styles/kendo.material.mobile.min.css" />
- <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/jquery.min.js"></script>
- <script src="https://kendo.cdn.telerik.com/2018.3.1017/js/kendo.all.min.js"></script>
- <div class="col-lg-12">
- <div id="example">
- <textarea id="editor"> </textarea>
- </div>
- <br />
- <div class="text-right">
- <input type="button" class="btn btn-primary" id="btnSave" value="Save" />
- </div>
- </div>
- < script type = "text/javascript" > $(document).ready(function() {
- $("#editor").kendoEditor({
- tools: ["bold", "italic", "underline", "strikethrough", "justifyLeft", "justifyCenter", "justifyRight", "justifyFull", "insertUnorderedList", "insertOrderedList", "indent", "outdent", "createLink", "unlink", "insertImage", "insertFile", "subscript", "superscript", "tableWizard", "createTable", "addRowAbove", "addRowBelow", "addColumnLeft", "addColumnRight", "deleteRow", "deleteColumn", "viewHtml", "formatting", "cleanFormatting", "fontName", "fontSize", "foreColor", "backColor", "print", "pdf"],
- pdf: {
- fileName: "PDFDemoDocument.pdf",
- proxyURL: "https://demos.telerik.com/kendo-ui/service/export",
- paperSize: "a4",
- margin: {
- bottom: 20,
- left: 20,
- right: 20,
- top: 20
- }
- }
- });
- $("#btnSave").click(function() {
- var content = $("#editor").val();
- var editorContent = new Object();
- editorContent.HtmlContent = content;
- $.ajax({
- type: "POST",
- url: "/Editor/SaveHtmlToDatabase",
- data: editorContent,
- dataType: "json",
- success: function(response) {
- Alert('Success');
- },
- });
- alert(content);
- });
- });
- < /script>
Complete Controller
- using KendoUI_Editor.Models;
- using System.Web.Mvc;
- using System.Linq;
- namespace KendoUI_Editor.Controllers {
- public class EditorController: Controller {
-
- public ActionResult Index() {
- return View();
- }
- [HttpPost]
- public void SaveHtmlToDatabase(EditorContent editorContent) {
- CSharpCornerEntities cSharpCornerEntities = new CSharpCornerEntities();
- cSharpCornerEntities.EditorContents.Add(editorContent);
- cSharpCornerEntities.SaveChanges();
- }
- }
- }
Refer to the attached project for reference. I have attached the demonstrated project without a package due to the size limit.
Summary
In this article, we saw how to work with Editors, how to store the HTML content into the database using Kendo UI in ASP.MVC5. I hope it helps you out. Your valuable feedback and comments about this article are always welcome.