I have an MVC application with custom view engine implementi开发者_如何转开发ng theme selection, i.e. dynamic setting of views' master pages. The app structure is described here The issue is that master pages exist out of their default location in Views folder. (That is because I want all theme-related files, ie. css and images to be at one place together with master. Putting css into Views would break security issue - default views' web.config won't allow to access anything there and I'd rather keep it that way.) Everything works fine until I try to change master page to be strongly typed, ie specify
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MyApp.Models.BaseModel>" %>
Then the page throws error:
Could not load System.Web.Mvc.ViewMasterPage < MyApp.Models.BaseModel >
.
So, is there any way to import that namespace to where it's required? To change some setting in web.config (in Views folder, I assume) or elsewhere?
This is a guess... but you get errors along those lines when you try to use namespaces in your view code (e.g. for HTML Helpers) that the view engine can't reference, and you solve this by adding it to web.config in the section.
So perhaps this would fix it?
<add namespace="MyApp.Models"/>
You could always pass the information as ViewData and cast it back into a strong type. Or you could create a ViewDataFacade class so the casting is handled in the classes themselves.
Update:
The controller (or something similar in your controller base):
public class MyController
{
public ActionResult Index()
{
ViewData["MyComplexType"] = GetMyComplexType();
return View();
}
}
The ViewDataFacade class would be thus:
public class ViewDataFacade
{
public MyClass MyComplexType { get; set; }
public ViewDataFacade(ViewDataDictionary viewData)
{
//If it exists, cast the viewdata object as MyComplexType, or return a new instance.
this.MyComplexType = viewData["MyComplexType"] != null ? (MyClass)viewData["MyComplexType"] : new MyClass();
}
}
View:
<%-- Constructor for the ViewDataFacade takes the current ViewData --%>
<% ViewDataFacade viewData = new ViewDataFacade(ViewData); %>
<%-- Example of MyComplexType that is a collection, with a single item (MyProperty) --%>
<ul>
<% foreach(var item in viewData.MyComplexType) { %>
<li><%= item.MyProperty %></li>
<% } %>
</ul>
精彩评论