Products Technologies Demo Docs Blog Support Company

This blog post contains outdated information.

The cited code snippets may be workarounds, and be part of the official API in the meantime.

HTML5: Saving Documents in an MVC Controller Method

Web.TextControl integrates into ASP.NET MVC by rendering the editor within an ActionResult. Documents save from ASPX code-behind as RTF strings and post to an HttpPost controller method via WebRequest. The controller receives both the document name and content as encoded byte arrays.

HTML5: Saving Documents in an MVC Controller Method

Web.TextControl is a Web Forms ASP.NET component that can be also integrated into MVC without breaking the MVC rules. This tutorial shows how to implement the HTML5 editor within ASP.NET MVC.

Basically, the ActionResult TemplateEditor executes an ASPX page with the Web.TextControl in memory in order to render the content as a string.

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
        return View(); 
    } 
 
    public ActionResult TemplateEditor() 
    { 
        var myString = RenderViewToString("Editor.aspx"); 
        return Content(myString); 
    } 
 
    protected string RenderViewToString(string viewPath) 
    { 
        System.IO.StringWriter htmlStringWriter = 
            new System.IO.StringWriter(); 
        Server.Execute(viewPath, htmlStringWriter); 
        return htmlStringWriter.GetStringBuilder().ToString(); 
    } 
}

In this concept, we can still use code-behind to load and save documents without losing the Controller routes and logic. But when saving a document inside the ASPX page, we might want to know that in the MVC Controller in order to store the data in a database.

Therefore, the Controller gets a new HttpPost method SaveDocument:

[HttpPost]
public bool SaveTemplate(string documentName, string document)
{
    // these are the values coming from the HTTP Post action
    // of the ASPX page
    string docName = documentName;
    string doc = document;

    // the document can now be saved in your Controller action
    // ...

    // return true, if successful
    return true;
}

This method accepts two strings the document name and the document itself in the RTF format. In the ASPX page, the document is saved on a button click within an AJAX UpdatePanel:

protected void Button1_Click(object sender, EventArgs e)
{
    // save the document to a string variable
    var RTFDocument = String.Empty;
    TextControl1.SaveText(out RTFDocument,
        TXTextControl.Web.StringStreamType.RichTextFormat);

    // create a new WebRequest
    WebRequest request = WebRequest.Create(
        "http://" + Request.Url.Authority + "/Home/SaveTemplate");

    // set the POST data
    var postData = "documentName=" + TextBox1.Text;
        postData += "&document=" + RTFDocument;
    
    // encode the data as an byte[] array
    var data = Encoding.ASCII.GetBytes(postData);

    // set the method, content type and length
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = data.Length;

    // write the data to the request stream
    using (var stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }

    // get response from HttpPost method
    WebResponse response = (HttpWebResponse)request.GetResponse();

    // read the data and check for return value "True"
    var dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);

    string sReturnValue = reader.ReadToEnd();

    if (sReturnValue == "True")
        lblInfo.Visible = true;
}

In code-behind, the document is saved as RTF in a string variable and a new WebRequest object is used to call the new Controller method SaveDocument. The document name and the document itself are encoded as a byte[] array and posted to the Controller.

This way, you can synchronize your Controller logic and save documents inside your Controller code.

We didn't release the official roadmap for version X13 yet, but this we can already announce: We will release an MVC version of Web.TextControl soon including a Nuget package and a pure MVC model.

Download the sample from GitHub and test it on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2012 or better
  • TX Text Control .NET Server (trial sufficient)

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETGitHubHTML5

MVC: Loading Files from the Backstage Menu

An MVC partial backstage view lists available files from a directory using a simple document model. When a user clicks a file, JavaScript sends an AJAX request to an HttpPost controller method…


ASP.NETGitHubHTML5

MVC: Replace the File Menu with a Backstage View Menu

Replace the Web.TextControl File menu with a full-page backstage view using JavaScript and CSS. The tutorial disables the default FILE ribbon handler, renders a vertical navigation panel with…


ASP.NETGitHubHTML5

MVC: Replace the Ribbon Table Menu with a Quick Insert Table Drop-down

Replace the default Web.TextControl ribbon table button with a grid-based quick insert dropdown using JavaScript. The tutorial renders a visual row-and-column picker, sends the selected dimensions…


ASP.NETGitHubHTML5

MVC: Arrange a Docked Web.TextControl with a Custom Bar at the Top

Position a docked Web.TextControl below a custom toolbar bar in an MVC view by using CSS and JavaScript to offset the editor. The tutorial wraps the control in a DIV, sets absolute positioning…


ASP.NETGitHubHTML5

MVC: Autosave and Restore Documents to and from the Local Browser Storage

Web.TextControl documents are autosaved to browser localStorage at five-second intervals using the JavaScript saveDocument method. On page reload, the ribbonTabsLoaded event checks for stored…

Share on this blog post on: