Problem with passing Model into compiled Razor View
I have a problem, with i compiled my Razor Views and try to pass here model. So, this is an 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 plugin .cs file (Method of controller):
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 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 into this method (.cs file of 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 can see here, it works: http://www.java2s.com/Open-Source/ASP.NET/Forum/openforum/OpenForum/Core/Views/Forum/Index.cs.htm (passing Model into compiled View, but i have this strange error =)
I changed code like this:
public class _Page_index_cshtml : System.Web.Mvc.WebViewPage<dynamic>
{
#line hidden
public _Page_index_cshtml()
{
}
开发者_JAVA百科 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: Object reference not set to an instance of an object.
So, i tried to check ViewModel and ViewModel.foo on null and it's true. Looks like, problem with passing null, but why =\
So, this is the last point before 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 model is not null here.
But when i check it here, in 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>");
}
}
Model is null...
Ofc, it maybe works with interface, i haven't tried it yet, but what's the problem with simple object passing =\
Still don't know where is an error. Mb i have to register my passing viewmodel here:
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);
}
Looks like i have to do it, but dont know how =\
Can anybody help to pass Model into compiled Razor View?
You can use @viewbag.xxxx
Anything you want to pass to the "view", you just type viewbag.xxx
then in view, you can use @viewbag.xxx
.
It will show it, whether is it a view data or database data.
精彩评论