I have a problem, I compiled my Razor Views and will try to pass the model here. So this is the error:
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'
I have a .cs plugin file (controller method):
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); } }
And the plugin controller method:
public abstract class PluginController : Controller { protected string PluginDirectory;
So here I have an error:
return base.View(viewName, model);
I want to pass Model to this method (.cs file from my view)
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>"); } }
I changed the code as follows:
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);
And now there is an error: the reference to the object is not installed in the instance of the object.
So, I tried to check ViewModel and ViewModel.foo for null, and that is true. It looks like the problem is with null skipping, but why = \
So this is the last point before the error:
[HttpGet] public ActionResult Index() { return RelativeView("index.cshtml", new HelloWorldPluginViewModel()); }
other words:
public ViewResult RelativeView(string viewName, object model) { viewName = PluginDirectory + viewName; return base.View(viewName, model); }
And the model is not null here.
But when I check it here, in the Execute method:
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>"); } }
The model is null ...
Ofc, maybe it works with the interface, I have not tried it yet, but the problem is with the simplest transfer of an object = \
I donβt know where the error is. Mb I need to register my view model:
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 // eg '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); }
Looks like I have to do it, but donβt know how = \
Can anyone help pass the model into a compiled razor view?