This example demonstrates how to configure the Rich Text Editor control to load/save documents from/to a database.
Follow the steps bellow to configure the Rich Text Editor to work with a database:
-
Create a class that models a document. This class should be able to store document identifiers, file formats, and content:
public class DocumentInfo { [Key] public int ID { set; get; } [Column("documentName")] public string DocumentName { set; get; } [Column("documentBytes")] public byte[] DocumentBytes { set; get; } [Column("documentFormat")] public int DocumentFormat { set; get; } }
-
Register the database context and add a connection string.
-
In the
Index
action method, load a document from the database and save the document to a model:public IActionResult Index() { var model = _context.Docs.FirstOrDefault(); if (model == null) model = new DocumentInfo(); return View(model); }
-
Create the Rich Text Editor and call its Open method to open the document stored in the model:
@(Html.DevExpress().RichEdit("richEdit") .Width("100%") .Height(800) .ExportUrl(Url.Action("SaveDocument")) .Open(() => { return Model.DocumentBytes; }, (DevExpress.AspNetCore.RichEdit.DocumentFormat)Model.DocumentFormat) )
-
Implement an action method that saves the document opened in the Rich Text Editor control back to the database. Assign this action method to the control's ExportUrl property:
public IActionResult SaveDocument(string base64, string fileName, int format, string reason) { byte[] fileContents = System.Convert.FromBase64String(base64); var doc = _context.Docs.FirstOrDefault(); if (doc == null) { doc = new DocumentInfo(); doc.DocumentBytes = fileContents; doc.DocumentFormat = format; doc.DocumentName = fileName; _context.Docs.Add(doc); } else { doc.DocumentBytes = fileContents; doc.DocumentFormat = format; doc.DocumentName = fileName; } _context.SaveChanges(); return Ok(); }
(you will be redirected to DevExpress.com to submit your response)