Example T352035
Visible to All Users

Rich Text Editor for ASP.NET MVC - How to open and save documents from a database

This code example demonstrates how to open and save RichEdit documents from a database binary column.

Implementation Details

Open a document

  • Pass a model with a binary property (rich text content to be displayed) to the RichEdit's PartialView.
  • Call the RichEditExtension.Open method to open a new document with the specified document ID and content type, and retrieve the binary content from the passed model:
C#
@Html.DevExpress().RichEdit(settings => { settings.Name = "RichEditName"; settings.CallbackRouteValues = new { Controller = "Home", Action = "RichEditPartial" }; //... }).Open(Model.DocumentId, Model.DocumentFormat, () => { return Model.Document; }).GetHtml()

Save a document

C#
settings.Saving = (s, e) => { byte[] docBytes = RichEditExtension.SaveCopy("RichEditName", DevExpress.XtraRichEdit.DocumentFormat.Rtf); DXWebApplication1.Models.DataHelper.SaveDocument(docBytes); e.Handled = true; };

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

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

Example Code

DXWebApplication1/Views/Home/RichEditPartial.cshtml
Razor
@model DXWebApplication1.Models.RichEditData @Html.DevExpress().RichEdit(settings => { settings.Name = "RichEditName"; settings.CallbackRouteValues = new { Controller = "Home", Action = "RichEditPartial" }; settings.Width = System.Web.UI.WebControls.Unit.Percentage(100); settings.Saving = (s, e) => { byte[] docBytes = RichEditExtension.SaveCopy("RichEditName", DevExpress.XtraRichEdit.DocumentFormat.Rtf); DXWebApplication1.Models.DataHelper.SaveDocument(docBytes); e.Handled = true; }; }).Open(Model.DocumentId, Model.DocumentFormat, () => { return Model.Document; }).GetHtml()
DXWebApplication1/Views/Home/RichEditPartial.vbhtml
Code
@ModelType DXWebApplication1.Models.RichEditData @Html.DevExpress().RichEdit( _ Sub(settings) settings.Name = "RichEditName" settings.CallbackRouteValues = New With {.Controller = "Home", .Action = "RichEditPartial"} settings.Width = System.Web.UI.WebControls.Unit.Percentage(100) settings.Saving = Sub(s, e) Dim docBytes As Byte() = RichEditExtension.SaveCopy("RichEditName", DevExpress.XtraRichEdit.DocumentFormat.Rtf) DXWebApplication1.Models.DataHelper.SaveDocument(docBytes) e.Handled = True End Sub End Sub).Open(Model.DocumentId, Model.DocumentFormat, _ Function() Return Model.Document End Function).GetHtml()
DXWebApplication1/Models/DataHelper.cs(vb)
C#
using System; using System.Linq; using DevExpress.XtraRichEdit; using DXWebApplication1.Models.EF; namespace DXWebApplication1.Models { public class DataHelper { public static byte[] GetDocument() { DataClassesDataContext context = new DataClassesDataContext(); return context.Docs.FirstOrDefault().DocBytes.ToArray(); } public static void SaveDocument(byte[] bytes) { DataClassesDataContext context = new DataClassesDataContext(); context.Docs.FirstOrDefault().DocBytes = bytes; context.SaveChanges(); } } public class RichEditData { public string DocumentId { get; set; } public DocumentFormat DocumentFormat { get; set; } public byte[] Document { get; set; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.