There are many examples out there which show how to store and retrieve virtual views from a file or database, but none of them show how to configure root web.config so that the application works properly, when a view's content retrieved from a file and contains for instance @ViewBag.Title = "Some Title"
line.
In this case I get CS0103: The name 'ViewBag' does not exist in the current context error, just like in cases when I remove web.config from ~/Views project folder.
So, what config elements from the ~/Views/web.config to root web.config in order to fix the mentioned above error?
Is it possible to load config elements into assemby at run time?
The problem is related to the fact that in case of a virtual view, a view whose content is retrieved from a file or database, inherits from System.Web.Mvc.ViewStartPage
that contains Html and Url, but doesn't ViewBag or Ajax properties. The virtual view is referred as follows
public class DynamicViewsController : Controller
{
public ActionResult GetView()
{
//SomeModel model = new SomeModel() { Count = 1 };
return View("/Virtual/VirtualViewToShow.cshtml");
}
}
A view located in ~/Views/ folder inherits from System.Web.Mvc.WebViewPage, which has ViewBag and ViewData, and Ajax properties defined.
The workaround to change the action method as follow
public PartialViewResult GetView()
{
//SomeModel model = new SomeModel() { Count = 1 };
return PartialView("/Virtual/VirtualViewToShow.cshtml");
}
and to add
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
to the text of the virtual view in order to load the layout also.
I don't know the reason why a virtual view inherits from System.Web.Mvc.ViewStartPage
, but I'll try to find the reason for this in another thread and will post the link when thre thread is created.
Edited
So a said the link to the new created thread is here
"Show Complete Compilation Source" screenshot attached