简体   繁体   English

ASP.NET MVC2 - ViewPage上的自定义属性

[英]ASP.NET MVC2 - Custom properties on a ViewPage

What I thought should be a fairly simple search, turned out to be alot more. 我认为应该是一个相当简单的搜索,结果是更多。

Atm I'm using a baseclass(MasterModel) for all my Models, that then get passed down from the ViewPage< HomeIndexModel > to the ViewMasterPage< MasterModel > and everything works fine. Atm我正在为我的所有模型使用基类(MasterModel),然后从ViewPage <HomeIndexModel>传递到ViewMasterPage <MasterModel>,一切正常。 This was done this way after reading a post by "Scott Gu". 这是在阅读“Scott Gu”的帖子后以这种方式完成的。

Then I thought about inheriting from the ViewPage and extending the Factory or where ever the ViewPage is built from ... but then I got lost. 然后我考虑从ViewPage继承并扩展Factory或从哪里构建ViewPage ......但后来我迷路了。

Who is responsible for instantiating the ViewPage, ViewMasterPage and ViewUserControl. 谁负责实例化ViewPage,ViewMasterPage和ViewUserControl。

The problem is more or less that I'm using an instance of the same class on 99% of all pages and always on the MasterPage, so its a pain to keep passing it around in the model all the time. 问题或多或少是因为我在99%的所有页面上使用同一个类的实例并且总是在MasterPage上,因此在模型中始终传递它是一件痛苦的事情。

Is this even the right way to go or do you guys have any other suggestions ? 这是否是正确的方式,或者你们有任何其他建议吗?

Update 更新

I need to be able to inject complex types taken from my IOC ( StructureMap ) into the constructor of the ViewPage, so it will be easy to change the implementation. 我需要能够将从IOC(StructureMap)中获取的复杂类型注入到ViewPage的构造函数中,因此很容易更改实现。 Thats why I'm looking for the place where the ViewPage gets constructed. 这就是为什么我在寻找构建ViewPage的地方。

Are you able to look at using MVC3? 你能看到使用MVC3吗? Dependency injection into view pages is not possible because the creation is buried deep inside the implementation of the view engine. 视图页面中的依赖注入是不可能的,因为创建深埋在视图引擎的实现中。

This is now addressed in MVC3. 现在在MVC3中解决了这个问题。 Full Details: http://bradwilson.typepad.com/blog/2010/07/service-location-pt3-views.html 完整详情: http//bradwilson.typepad.com/blog/2010/07/service-location-pt3-views.html

As I understand it, you are responsible, because within the definition of your View you specifically state what is the base class of your view. 据我了解,您有责任,因为在View的定义中,您明确说明了视图的基类。

Try this: 尝试这个:

public sealed class MyViewPage : System.Web.Mvc.ViewPage
{
  public string Herp {get{return "Derp";}}
}

and, in your view, do this: 并且,在您看来,这样做:

<%@ Page Language="C#" Inherits="MyApplication.MyViewPage" %>
<!-- compare the Inherits attribute in your standard View page -->
<%: Herp %>

I've never done this, but I'm 90% sure it would work. 我从来没有这样做,但我90%肯定它会起作用。 You would also need to do a generic version as well: 您还需要执行通用版本:

public sealed class MyViewPage<T> : System.Web.Mvc.ViewPage<T> {}

I'd ditch the base view model and DI into ViewPage to use composition (via ViewData[]) instead because it allows you a lot more flexibility than inheritance of view models. 我将基本视图模型和DI放弃到ViewPage中以使用合成(通过ViewData []),因为它允许您比继承视图模型更灵活。 Imagine if you later wanted to add more components (header, menus, sidebar, user profile widget, etc) to various pages. 想象一下,如果您以后想要向各种页面添加更多组件(标题,菜单,侧边栏,用户配置文件小部件等)。 If you have to fit all that into your base view model it's going to get crowded and you probably won't even need everything on every view. 如果你必须将所有这些都融入到基本视图模型中,那么它将变得拥挤,你可能甚至不需要每个视图上的所有内容。 I use the model purely for the specific view being rendered and then put my other components into ViewData. 我纯粹为渲染的特定视图使用该模型,然后将我的其他组件放入ViewData。 Sample view code: 示例视图代码:

<% var headerModel = ViewData[Constants.HeaderData] as HeaderViewModel %>

Or the super easy solution: have you considered just using ObjectFactory in your view? 或者超级简单的解决方案:您是否考虑过在视图中使用ObjectFactory (This would make some people --myself included-- cringe, but it does work.) (这会让一些人 - 包括我自己 - 畏缩,但确实有效。)

ObjectFactory.GetInstance<IService>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM