How do you switch View Engine on the fly within an ASP.Net MVC Controller action?
3 answers
I would create a custom ActionResult. I am using the Json () function to return a JsonResult when I need JSON as a response. I am using this code to populate ExtJS tree using JSON data.
public JsonResult Folders(string node)
{
var relativePath = (node == "root") ? "" : node;
var path = Path.Combine(BASE_PATH, relativePath);
var folder = new DirectoryInfo(path);
var subFolders = folder.GetDirectories();
var folders = new List<ExtJsTreeNode>();
foreach (var subFolder in subFolders)
{
folders.Add(new ExtJsTreeNode(subFolder.Name, subFolder.FullName.Replace(BASE_PATH, ""), "folder"));
}
return Json(folders);
}
private class ExtJsTreeNode
{
public string text { get; set; }
public string id { get; set; }
public string cls { get; set; }
public ExtJsTreeNode(string text, string id, string cls)
{
this.text = text;
this.id = id;
this.cls = cls;
}
}
An example of a custom ActionResult is here .
+1
source to share