Skip to content

Configure the Rich Text Editor control to load/save documents from/to a database.

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/asp-net-core-richedit-work-with-database

Repository files navigation

Rich Text Editor for ASP.NET Core - How to load/save documents from/to a database

This example demonstrates how to configure the Rich Text Editor control to load/save documents from/to a database.

Rich Text Editor

Overview

Follow the steps bellow to configure the Rich Text Editor to work with a database:

  1. 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; }
    }
  2. Register the database context and add a connection string.

  3. 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);
    }
  4. 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)
    )
  5. 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();
    }

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Configure the Rich Text Editor control to load/save documents from/to a database.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •