gpt4 book ai didi

asp.net-mvc - 将模型传递到已编译的 Razor View 中时出现问题

转载 作者:行者123 更新时间:2023-12-03 02:13:59 24 4
gpt4 key购买 nike

我有一个问题,我编译了 Razor View 并尝试在此处传递模型。所以,这是一个错误:

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文件( 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);
}
}

以及插件 Controller 方法:

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

我想将模型传递到此方法中(我的 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 (将模型传递到已编译的 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>");
}
}

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

<小时/>

所以,我尝试检查 ViewModel 和 ViewModel.foo 是否为 null,结果是正确的。看起来,传递 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);
}

这里模型不为空

但是当我在执行方法中检查它时:

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,它可能适用于接口(interface),我还没有尝试过,但是简单的对象传递有什么问题=\

还是不知道哪里出错了。Mb我必须在这里注册我的传递 View 模型:

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

看起来我必须这样做,但不知道如何=\

任何人都可以帮助将模型传递到编译的 Razor View 中吗?

最佳答案

您可以使用@viewbag.xxxx

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

它会显示出来,无论是 View 数据还是数据库数据。

关于asp.net-mvc - 将模型传递到已编译的 Razor View 中时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7222925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com