1

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?

2 Answers 2

1

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 attachedenter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Follow as mentioned step and you are done!

  • Merge <configSections> section of ~/views/web.config with root level web.config
  • Now just move <system.web.webPages.razor> section from ~/views/web.config to root level web.config
  • Cheers :) its done.
  • You should also merge <appSettings>, <system.web> & <system.webServer> with root level web.config.

3 Comments

Of course I followed this steps before posting the question, but without success. Are there any additional steps I have to follow? Have it functioned in your case?
Yes it worked in my case... can you please elaborate your whole scenario?
The new thread is here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.