How do you switch View Engine on the fly within an ASP.Net MVC Controller action?

I want to write a custom view engine that returns custom text (like coma delimited). Does anyone know how I can change the view engine on the fly to handle this?

+1


source to share


3 answers


Your controller doesn't need to know or worry about it, besides what kind of data to send. The view can display in any format. I have views that emit RSS (XML) etc. In the controller, either send it to the default view or explicitly specify the target view.



0


source


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


If I understand your question correctly, you want to use different views based on the parameters passed to the controller. If so, you can use this statement in a controller action:

return View("ViewName");

      

Otherwise, please clarify your question.

0


source







All Articles