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();
}
}
view raw Controller.cs hosted with ❤ by GitHub

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;
}
view raw Controller.cs hosted with ❤ by GitHub

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;
}
view raw Editor.aspx.cs hosted with ❤ by GitHub

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.