将视图数据传递到 ASP.NET MVC 母版页

发布于 2024-07-16 07:12:32 字数 532 浏览 4 评论 0原文

我正在尝试将 ViewData 传递到我的 asp.net mvc 母版页,以获取我保留在母版页上的 mvc 用户控件。 例如,我创建了一个名称下拉列表作为 mvc 用户控件,并将其放入我的母版页中。

我遇到的问题是将 ViewData 传递到母版页。 我发现微软的这篇文章有一个不错的解决方案,但我想知道是否还有其他“更好”的解决方案。 我不喜欢链接中的解决方案的是,我必须更改每个控制器以从新的控制器类继承。

http://www.asp.net/learn/MVC/tutorial-13 -cs.aspx

编辑:我正在考虑的问题是,如果我在依赖 ViewData 的母版页中放置一个用户控件,我必须重复 包括使用所述母版页的每个页面的 ViewData。 上面链接中的解决方案可能是最好的解决方案,但我希望还有其他替代方案。

I'm trying to pass ViewData to my asp.net mvc masterpage for an mvc usercontrol that I keep on a masterpage. For example, I created a dropdownlist of names as an mvc usercontrol and I put that in my masterpage.

The problem I am running into is passing the ViewData to the masterpage. I found this article from Microsoft which has a decent solution but I was wondering if there are other "better" solutions out there. The thing I don't like about the solution in the link is that I have to change every controller to inherit from a new controller class.

http://www.asp.net/learn/MVC/tutorial-13-cs.aspx

Edit: The problem I am looking at is the fact that if I place a user control in my masterpage that relies on ViewData, I have to REPEATEDLY include that ViewData for every single page that uses said masterpage. It's possible the solution in the link above is the best solution but I was hoping there were other alternatives.

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

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

发布评论

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

评论(6

我一向站在原地 2024-07-23 07:12:32

母版页已经可以访问 ViewData。 如果您想要对其进行强类型访问,则需要做两件事:

  1. 将母版页内容放入基类中(例如 CommonViewData)
  2. 您的母版页是否从通用 ViewMasterPage 继承<>? 类:

    " %>

The master page already has access to the ViewData. If you want strongly typed access to it, you need to do two things:

  1. Put the master page stuff in a base class (e.g. CommonViewData)
  2. Have you master page inherit from the generic ViewMasterPage<> class:

    " %>

破晓 2024-07-23 07:12:32

您是否可以在基本控制器类上使用 OnActionExecuting 方法并在那里填充视图数据?

比如:

protected override void OnActionExecuting(ActionExecutingContext context)
{
  context.Controller.ViewData.Model = GetDataForControl();
}

我还没有尝试过,所以这只是一个想法......

Could you possibly use the OnActionExecuting method on a base controller class and populate the view data there?

Something like:

protected override void OnActionExecuting(ActionExecutingContext context)
{
  context.Controller.ViewData.Model = GetDataForControl();
}

I haven't tried it so it's just a thought...

温柔一刀 2024-07-23 07:12:32

无论如何,我在当前项目中使用了该教程中的方法,并且效果非常好。

如果数据有些静态(例如变化不大的菜单),您还可以做的是将对象放在缓存中,这样每次控制器初始化时就不会调用数据库。

For what it's worth, I am using the method from that tutorial in a current project and it works very well.

What you can also do, if it is data that is somewhat static (like a menu that doesn't change much), is to put the object on the cache so your database isn't called for every controller initialisation.

睫毛溺水了 2024-07-23 07:12:32

我通常为我的 MasterPage 使用抽象控制器类,这是最好的解决方案,因为 MasterPage 就像一个“抽象视图”。 但我重写了 MasgerPageController View() 方法以包含视图数据。

protected override ViewResult View(string viewName, string masterName, object model)
{
    this.ViewData["menu"] = this.PagesRepository.GetPublishPages();
    return base.View(viewName, masterName, model);
}

I usually use an abstract controller class for my MasterPage, it is the best solution, because the MasterPage is like an "abstract view". But I override the MasgerPageController View() method to include the viewdata.

protected override ViewResult View(string viewName, string masterName, object model)
{
    this.ViewData["menu"] = this.PagesRepository.GetPublishPages();
    return base.View(viewName, masterName, model);
}
夏雨凉 2024-07-23 07:12:32

我不太明白你的问题...

我正在考虑的问题是,如果我在依赖 ViewData 的母版页中放置一个用户控件,我必须重复为使用所述母版页的每个页面包含该 ViewData。

嗯,是的……你当然知道。 如果您的母版页中有一个用户控件,那么您当然必须为每个操作和操作传递该用户控件所需的数据。 使用该母版页的视图。

如果您只是从基本控制器继承,那么您不必重复自己。

您的问题是否是某些控制器的操作既调用又不调用从该特定母版页派生的视图? 因此,如果您正在实现一个基本控制器,不使用该特定母版页的操作仍将具有它的视图数据......? (如果这一切都有意义的话;-)

I don't quite get your problem...

The problem I am looking at is the fact that if I place a user control in my masterpage that relies on ViewData, I have to REPEATEDLY include that ViewData for every single page that uses said masterpage.

Well yeah... of course you do. If you have a usercontrol in your master page then of course you're going to have to pass the required data for that usercontrol for every action & view that uses that masterpage.

It's not like you have to repeat yourself if you are just inheriting from a base controller.

Is your issue the fact that some controllers have actions that both do and don't call views that derive from that particular masterpage? So therefore if you are implementing a base controller, the actions that don't use that particular masterpage will still have the viewdata for it...? (If all that makes sense ;-)

御守 2024-07-23 07:12:32

我认为建议的解决方案确实有效,但不是理想的解决方案。 如果我们将代码放在 BaseController 构造函数中,即使没有 MasterPages 的 Action 方法(例如 Post 方法和 Ajax 方法)也会调用它。
我认为更好的解决方案(不理想)是在母版页中调用 Html.Action 方法。

I think the solution suggested does work but not the ideal solution. If we put the code in the BaseController constructor it is going to be called even for Action methods which does not have a MasterPages (e.g Post methods and Ajax methods).
I think a better solution(not ideal) is to call Html.Action method in the Master page.

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