Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with passing Model into compiled Razor View
    primarykey
    data
    text
    <p>I have a problem, with i compiled my Razor Views and try to pass here model. So, this is an error:</p> <pre><code>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' </code></pre> <p>I have plugin .cs file (Method of controller):</p> <pre><code>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); } } </code></pre> <p>And plugin controller method:</p> <pre><code>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); } } </code></pre> <p>So, here i have an error:</p> <pre><code>return base.View(viewName, model); </code></pre> <p>I want to pass Model into this method (.cs file of my View)</p> <pre><code> public class _Page_index_cshtml : System.Web.Mvc.WebViewPage&lt;MvcApplication2.PluginHelloWorld.HelloWorldPluginViewModel&gt; { #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&lt;h2&gt;Hello!&lt;/h2&gt;\r\n&lt;p&gt;"); Write(Html.TextBox("hello", Model.foo)); WriteLiteral("&lt;/p&gt;"); } } </code></pre> <hr> <h2>As i can see here, it works: <a href="http://www.java2s.com/Open-Source/ASP.NET/Forum/openforum/OpenForum/Core/Views/Forum/Index.cs.htm" rel="nofollow">http://www.java2s.com/Open-Source/ASP.NET/Forum/openforum/OpenForum/Core/Views/Forum/Index.cs.htm</a> (passing Model into compiled View, but i have this strange error =)</h2> <p>I changed code like this:</p> <pre><code> public class _Page_index_cshtml : System.Web.Mvc.WebViewPage&lt;dynamic&gt; { #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&lt;h2&gt;Hello!&lt;/h2&gt;\r\n&lt;p&gt;"); Write(ViewModel.foo); //Write(Html.TextBox("hello", ViewModel.foo)); WriteLiteral("&lt;/p&gt;"); } } </code></pre> <p>And now there is an error: Object reference not set to an instance of an object.</p> <hr> <h2>So, i tried to check ViewModel and ViewModel.foo on null and it's true. Looks like, problem with passing null, but why =\</h2> <p>So, this is the last point before error:</p> <pre><code>[HttpGet] public ActionResult Index() { return RelativeView("index.cshtml", new HelloWorldPluginViewModel()); } </code></pre> <p>other words:</p> <pre><code>public ViewResult RelativeView(string viewName, object model) { viewName = PluginDirectory + viewName; return base.View(viewName, model); } </code></pre> <p>And model <strong>is not null</strong> here.</p> <p>But when i check it here, in Execute method:</p> <pre><code>public class _Page_index_cshtml : System.Web.Mvc.WebViewPage&lt;MvcApplication2.PluginHelloWorld.HelloWorldPluginViewModel&gt; { #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&lt;h2&gt;Hello!&lt;/h2&gt;\r\n&lt;p&gt;"); Write(Html.TextBox("hello", Model.foo)); WriteLiteral("&lt;/p&gt;"); } } </code></pre> <p>Model <strong>is null</strong>...</p> <h2>Ofc, it maybe works with interface, i haven't tried it yet, but what's the problem with simple object passing =\</h2> <p>Still don't know where is an error. Mb i have to register my passing viewmodel here:</p> <pre><code>protected void Application_Start() { container = new WindsorContainer(); // register 'normal' controllers container.Register(AllTypes.FromThisAssembly().BasedOn&lt;IController&gt;().If(t =&gt; t.Name.EndsWith("Controller")).Configure((ConfigureDelegate)(c =&gt; 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&lt;string, Type&gt; pluginTypes = new Dictionary&lt;string, Type&gt;(); // 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 =&gt; 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); } </code></pre> <h2>Looks like i have to do it, but dont know how =\</h2> <p>Can anybody help to pass Model into compiled Razor View?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload