将Model传递给已编译的Razor视图的问题

时间:2011-08-28 18:17:32

标签: asp.net-mvc asp.net-mvc-3

我有一个问题,我编译了我的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>");
        }
    }

正如我在这里看到的,它可以工作:http://www.java2s.com/Open-Source/ASP.NET/Forum/openforum/OpenForum/Core/Views/Forum/Index.cs.htm(将Model传递给已编译的View,但我有这个奇怪的错误=)

我改变了这样的代码:

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

现在出现错误:对象引用未设置为对象的实例。


因此,我尝试在null上检查ViewModel和ViewModel.foo,这是真的。看起来像传递null的问题,但为什么= \

所以,这是错误之前的最后一点:

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

模型为空 ...

Ofc,它可能适用于界面,我还没有尝试过,但是简单对象传递的问题是什么= \

仍然不知道哪里出错。 我必须在这里注册我的传递视图模型:

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吗?

1 个答案:

答案 0 :(得分:-3)

您可以使用@viewbag.xxxx

您要传递给“视图”的任何内容,只需输入viewbag.xxx即可 然后在视图中,您可以使用@viewbag.xxx

它将显示它,无论是视图数据还是数据库数据。