Happy New Year, everybody!

In the last blog entry, we showed how to replace the file menu with an MS Word-style backstage menu. This project shows how to load documents from a partial view in the backstage menu.

MVC: Loading files from the backstage menu

The partial backstage view Open.cshtml uses a very simple model to describe a document:

public class Document
public Document() { }
public Document(string FilePath)
this.FilePath = FilePath;
this.Name = Path.GetFileName(this.FilePath);
public string Name { get; }
public string FilePath { get; set; }
view raw Document.cs hosted with ❤ by GitHub

In the partial view, a list item is created for each file of the given model:

@foreach (var item in Model) {
<a onclick="LoadFromController('@item.Name')" class="right" href="#">
<img src="~/img/doc.png" /><strong>@item.Name</strong>
if (Path.GetDirectoryName(item.FilePath).Length > 50)
Path.GetDirectoryName(item.FilePath).Length - 50);
view raw Open.cshtml hosted with ❤ by GitHub

The controller ActionResult GetView iterates through all files of a given directory in order to create the view model that is used in the above partial view:

public ActionResult GetView(string viewName)
switch (viewName)
case "Open":
var documents = new List<Document>();
foreach (string file in Directory.GetFiles(
Document doc = new Document(file);
return PartialView("Stageviews/Open", documents);
return PartialView("Stageviews/" + viewName);
view raw HomeController.cs hosted with ❤ by GitHub

If a file is clicked in the view, the following Javascript loads a document from the controller HttpPost method LoadTemplate:

function LoadFromController(DocumentName) {
var serviceURL = "/Home/LoadTemplate";
type: "POST",
url: serviceURL,
contentType: 'application/json',
data: JSON.stringify({
DocumentName: DocumentName
success: successFunc,
error: errorFunc
function successFunc(data, status) {
TXTextControl.streamType.InternalUnicodeFormat, data);
function errorFunc() {
alert('File could not be loaded');
view raw tx.js hosted with ❤ by GitHub

The controller method LoadTemplate loads the selected file into a temporary ServerTextControl and returns the document in the internal TX Text Control format which is loaded client-side using Javascript into TX Text Control.

public string LoadTemplate(LoadDocumentViewModel model)
byte[] data;
using (TXTextControl.ServerTextControl tx =
new TXTextControl.ServerTextControl())
TXTextControl.StreamType streamType =
case ".doc":
streamType = TXTextControl.StreamType.MSWord;
case ".rtf":
streamType = TXTextControl.StreamType.RichTextFormat;
case ".tx":
streamType = TXTextControl.StreamType.InternalUnicodeFormat;
case ".pdf":
streamType = TXTextControl.StreamType.AdobePDF;
tx.Load(Server.MapPath("/App_Data/documents/" +
model.DocumentName), streamType);
tx.Save(out data,
return Convert.ToBase64String(data);
view raw HomeController.cs hosted with ❤ by GitHub

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