视图模型 (ViewData)、用户控件/部分和全局变量 - 最佳实践?

发布于 2024-07-25 04:02:10 字数 1118 浏览 5 评论 0原文

我正在尝试找出一种好方法,让“全局”成员(例如 CurrentUser、主题等)出现在我的所有部分以及我的视图中。

我不想有一个可以返回此数据的逻辑类(如 BL.CurrentUser),我确实认为它需要成为我视图中模型的一部分,所以我尝试使用这些成员从 BaseViewData 继承。 在我的控制器中,以这种或另一种方式(我的 BaseController 中的过滤器或基本方法),我创建继承类的实例并将其作为视图数据传递。 到目前为止,一切都很完美,因为那时我可以在主视图上使用基本成员查看视图数据。 但是部分内容呢?

如果我有一个需要显示博客文章的简单部分,那么它看起来像这样:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="ViewUserControl<Post>" %>

在我的视图中渲染此部分的简单代码(其 model.Posts 是 IEnumerable):

<%foreach (Post p in this.Model.Posts) {%>
    <%Html.RenderPartial("Post",p); %>
<%}%>

以及 部分模型不是 BaseViewData,我无权访问这些属性。 因此,我尝试创建一个名为 PostViewData 的类,它继承自 BaseViewData,但是我的包含视图将有一个代码来实际在其中创建 PostViewData,以便将其传递给部分:

<%Html.RenderPartial("Post",new PostViewData { Post=p,CurrentUser=Model.CurrentUser,... }); %>

或者我可以使用复制构造函数,

<%Html.RenderPartial("Post",new PostViewData(Model) { Post=p }); %>

我只是想知道在我继续之前是否有其他方法可以实现这一点。

有什么建议么?

谢谢!

I'm trying to figure out a good way to have 'global' members (such as CurrentUser, Theme etc.) in all of my partials as well as in my views.

I don't want to have a logic class that can return this data (like BL.CurrentUser) I do think it needs to be a part of the Model in my views So I tried inheriting from BaseViewData with these members. In my controllers, in this way or another (a filter or base method in my BaseController), I create an instance of the inheriting class and pass it as a view data. Everything's perfect till this point, cause then I have my view data available on the main View with the base members. But what about partials?

If I have a simple partial that needs to display a blog post then it looks like this:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="ViewUserControl<Post>" %>

and simple code to render this partial in my view (that its model.Posts is IEnumerable<Post>):

<%foreach (Post p in this.Model.Posts) {%>
    <%Html.RenderPartial("Post",p); %>
<%}%>

Since the partial's Model isn't BaseViewData, I don't have access to those properties. Hence, I tried to make a class named PostViewData which inherits from BaseViewData, but then my containing views will have a code to actually create the PostViewData in them in order to pass it to the partial:

<%Html.RenderPartial("Post",new PostViewData { Post=p,CurrentUser=Model.CurrentUser,... }); %>

Or I could use a copy constructor

<%Html.RenderPartial("Post",new PostViewData(Model) { Post=p }); %>

I just wonder if there's any other way to implement this before I move on.

Any suggestions?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-08-01 04:02:11

您是否考虑过将这些内容保留在会话中,并在会话周围编写一个强类型包装器,以便您可以访问这些信息? 然后,在任何视图中,您可以简单地使用 ViewPage 的(或 ViewUserControl 的)Session 属性创建一个新的包装类并使用它。

Have you considered keeping these things in the session and writing a strongly-typed wrapper around the session that will give you access to this information? Then in any view you can simply create a new wrapper class with the ViewPage's (or ViewUserControl's) Session property and use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文