Problem transferring Models to compiled Razor View

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; // 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); } } 

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>"); } } 

As I see here, it works: http://www.java2s.com/Open-Source/ASP.NET/Forum/openforum/OpenForum/Core/Views/Forum/Index.cs.htm (passing the model to a compiled view, but I have this strange error =)

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); //Write(Html.TextBox("hello", ViewModel.foo)); WriteLiteral("</p>"); } } 

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?

+4
source share
1 answer

You can use @viewbag.xxxx

All that you want to convey in the "view", just enter viewbag.xxx then you can use @viewbag.xxx .

It will show whether it will be view data or database data.

-3
source

Source: https://habr.com/ru/post/1369397/


All Articles