Reputation: 3856
I have an ASP.NET MVC application that I need to add content management pages to. Since it's CMS the path of the pages are going to be variable. I am trying to figure out how to get ASP.NET to display a view that is variable. What does the controller look like?
The code would be something like this: Dim MyView As New System.Web.Mvc.ViewResult() Return MyView
But that doesn't let me define the content of the view, which I'm not sure how to do. The view does not physically exist; I need to dynamically generate it from data within my database. I am storing the HTML in the database.
Upvotes: 0
Views: 171
Reputation: 5048
You can create your content "by hand", without Razor views and return Content
with the generated HTML instead of View
in your action method.
EDIT:
Or, create a master view with all the common elements that accepts generated content as a string:
@model string
<html>
<!-- all the common stuff here -->
@Html.Raw(Model)
<!-- more common stuff -->
</html>
Your controllers would generate the HTML and pass it to this view:
public ActionResult Index()
{
var html = GenerateContent(); // or whatever
return ActionResult("MasterView", (object)html);
}
It's important to cast the second parameter to an object as otherwise it would be interpreted as layout location.
If you need to insert the generated content between common components you can pass a dictionary of strings as a model and render just the part you like, eg. Html.Raw(Model["footer"])
Upvotes: 1
Reputation: 1039498
What does the controller look like?
public ActionResult Index()
{
string dynamicViewName = "~/Views/Shared/FooBar.cshtml";
return View(dynamicViewName);
}
You could also specify a Layout if you wish:
public ActionResult Index()
{
string dynamicViewName = "~/Views/Shared/FooBar.cshtml";
string dynamicLayout = "~/Views/Shared/_SomeLayout.cshtml";
return View(dynamicViewName, dynamicLayout);
}
Upvotes: 0