我有一个问题,我编译了我的Razor Views并尝试传递这里的模型。 所以,这是一个错误:
The model item passed into the dictionary is of type 'MvcApplication2.PluginHelloWorld.HelloWorldPluginViewModel', but this dictionary requires a model item of type 'MvcApplication2.PluginHelloWorld.HelloWorldPluginViewModel'
我有插件.cs文件(控制器方法):
public class PluginHelloWorldController : PluginController
{
public PluginHelloWorldController(string pluginDirectory) : base(pluginDirectory) { }
[HttpGet]
public ActionResult Index()
{
return RelativeView("index.cshtml", new HelloWorldPluginViewModel());
}
[HttpPost]
public ActionResult Index(HelloWorldPluginViewModel model)
{
return RelativeView("index.cshtml", model);
}
}
插件控制器方法:
public abstract class PluginController : Controller
{
protected string PluginDirectory;
// pluginDirectory will have a value like '~/extensions/plugins/rating/'
public PluginController(string pluginDirectory)
{
if (pluginDirectory.EndsWith("/"))
{
PluginDirectory = pluginDirectory;
}
else
{
PluginDirectory = pluginDirectory + "/";
}
}
public ViewResult RelativeView(string viewName, object model)
{
viewName = PluginDirectory + viewName;
return base.View(viewName, model);
}
}
所以,我有一个错误:
return base.View(viewName, model);
我想将Model传递给此方法(我的View的.cs文件)
public class _Page_index_cshtml : System.Web.Mvc.WebViewPage<MvcApplication2.PluginHelloWorld.HelloWorldPluginViewModel>
{
#line hidden
public _Page_index_cshtml()
{
}
protected System.Web.HttpApplication ApplicationInstance
{
get
{
return ((System.Web.HttpApplication)(Context.ApplicationInstance));
}
}
public override void Execute()
{
WriteLiteral("\r\n<h2>Hello!</h2>\r\n<p>");
Write(Html.TextBox("hello", Model.foo));
WriteLiteral("</p>");
}
}
我改变了这样的代码:
public class _Page_index_cshtml : System.Web.Mvc.WebViewPage<dynamic>
{
#line hidden
public _Page_index_cshtml()
{
}
protected System.Web.HttpApplication ApplicationInstance
{
get
{
return ((System.Web.HttpApplication)(Context.ApplicationInstance));
}
}
public override void Execute()
{
var ViewModel = Model as MvcApplication2.PluginHelloWorld.HelloWorldPluginViewModel;
WriteLiteral("\r\n<h2>Hello!</h2>\r\n<p>");
Write(ViewModel.foo);
//Write(Html.TextBox("hello", ViewModel.foo));
WriteLiteral("</p>");
}
}
现在出现错误:对象引用未设置为对象的实例。
所以,这是错误之前的最后一点:
[HttpGet]
public ActionResult Index()
{
return RelativeView("index.cshtml", new HelloWorldPluginViewModel());
}
换句话说:
public ViewResult RelativeView(string viewName, object model)
{
viewName = PluginDirectory + viewName;
return base.View(viewName, model);
}
模型不为空。
但是当我在这里检查时,在Execute方法中:
public class _Page_index_cshtml : System.Web.Mvc.WebViewPage<MvcApplication2.PluginHelloWorld.HelloWorldPluginViewModel>
{
#line hidden
public _Page_index_cshtml()
{
}
protected System.Web.HttpApplication ApplicationInstance
{
get
{
return ((System.Web.HttpApplication)(Context.ApplicationInstance));
}
}
public override void Execute()
{
WriteLiteral("\r\n<h2>Hello!</h2>\r\n<p>");
Write(Html.TextBox("hello", Model.foo));
WriteLiteral("</p>");
}
}
模型为空 ...
仍然不知道哪里出错。 我必须在这里注册我的传递视图模型:
protected void Application_Start()
{
container = new WindsorContainer();
// register 'normal' controllers
container.Register(AllTypes.FromThisAssembly().BasedOn<IController>().If(t => t.Name.EndsWith("Controller")).Configure((ConfigureDelegate)(c => c.LifeStyle.Transient)));
// string is the route path
// type is the plugin controller type
// it maps routes like '/admin/plugins/rating/{action}' to Crash.PageRating.PageRatingController
Dictionary<string, Type> pluginTypes = new Dictionary<string, Type>();
// location of the plugins
var allPluginsDir = new DirectoryInfo(Server.MapPath("~/extensions/plugins/"));
foreach(var dir in allPluginsDir.GetDirectories())
{
string pluginDir = string.Format("~/extensions/plugins/{0}/", dir.Name);
// loop through all dll files, though only one should exist per directory
foreach(var dll in dir.GetFiles("*.dll"))
{
var assembly = Assembly.LoadFrom(dll.FullName);
// register compiled razor views
// e.g. 'settings.cshtml' is registered as '~/extensions/plugins/rating/settings.cshtml'
BoC.Web.Mvc.PrecompiledViews.ApplicationPartRegistry.Register(assembly, pluginDir);
// only one controller per plugin in this case
var controllerType = assembly.GetTypes().Where(t => typeof(PluginController).IsAssignableFrom(t)).FirstOrDefault();
if(controllerType != null)
{
// register controller
// pass pluginDir to the constructor
container.Register(Component.For(controllerType).DependsOn(new { pluginDirectory = pluginDir }).LifeStyle.Transient);
// admin route url
var pluginUrl = string.Format("plugins/{0}/{{action}}", dir.Name);
// map admin route to controller
pluginTypes.Add(pluginUrl, controllerType);
RouteTable.Routes.MapRoute("plugin_" + dir.Name, pluginUrl, new {controller=controllerType.Name.Replace("Controller",""),action="Index" },new[]{controllerType.Namespace});
}
}
}
AreaRegistration.RegisterAllAreas();
// Controller factory
var controllerFactory = new CrashControllerFactory(container.Kernel,pluginTypes);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
RegisterRoutes(RouteTable.Routes);
}
任何人都可以帮助将Model传递给已编译的Razor View吗?
答案 0 :(得分:-3)
您可以使用@viewbag.xxxx
您要传递给“视图”的任何内容,只需输入viewbag.xxx
即可
然后在视图中,您可以使用@viewbag.xxx
。
它将显示它,无论是视图数据还是数据库数据。