简体   繁体   English

将视图数据传递给 asp.net mvc masterpages

[英]passing viewdata to asp.net mvc masterpages

I'm trying to pass ViewData to my asp.net mvc masterpage for an mvc usercontrol that I keep on a masterpage.我正在尝试将 ViewData 传递给我的 asp.net mvc 母版页,以获取我保留在母版页上的 mvc 用户控件。 For example, I created a dropdownlist of names as an mvc usercontrol and I put that in my masterpage.例如,我创建了一个名称下拉列表作为 mvc 用户控件,并将其放在我的母版页中。

The problem I am running into is passing the ViewData to the masterpage.我遇到的问题是将 ViewData 传递给母版页。 I found this article from Microsoft which has a decent solution but I was wondering if there are other "better" solutions out there.我从 Microsoft 找到了这篇文章,它有一个不错的解决方案,但我想知道是否还有其他“更好”的解决方案。 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.我不喜欢链接中的解决方案是我必须更改每个 controller 以从新的 controller class 继承。

http://www.asp.net/learn/MVC/tutorial-13-cs.aspx 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.编辑:我正在查看的问题是,如果我在依赖于 ViewData 的母版页中放置一个用户控件,我必须为使用所述母版页的每个页面重复包含该ViewData It's possible the solution in the link above is the best solution but I was hoping there were other alternatives.上面链接中的解决方案可能是最好的解决方案,但我希望还有其他选择。

The master page already has access to the ViewData.母版页已经可以访问 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 (eg CommonViewData)将母版页内容放在基础 class 中(例如 CommonViewData)
  2. Have you master page inherit from the generic ViewMasterPage<> class:您是否从通用 ViewMasterPage<> class 继承母版页:

    " %> " %>

Could you possibly use the OnActionExecuting method on a base controller class and populate the view data there?您能否在基础 controller class 上使用OnActionExecuting方法并在那里填充视图数据?

Something like:就像是:

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

I haven't tried it so it's just a thought...我没试过所以只是一个想法......

I usually use an abstract controller class for my MasterPage, it is the best solution, because the MasterPage is like an "abstract view".我通常为我的 MasterPage 使用抽象 controller class,这是最好的解决方案,因为 MasterPage 就像一个“抽象视图”。 But I override the MasgerPageController View() method to include the viewdata.但是我重写了 MasgerPageController View() 方法以包含视图数据。

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

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.如果数据有点 static(就像一个变化不大的菜单),您还可以做的是将 object 放在缓存中,这样就不会为每个 Z594C103F2C6E04C3D8AB059F0C1 初始化调用您的数据库。

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 (eg Post methods and Ajax methods).如果我们将代码放在 BaseController 构造函数中,即使是没有 MasterPages 的 Action 方法(例如 Post 方法和 Ajax 方法)也会被调用。
I think a better solution(not ideal) is to call Html.Action method in the Master page.我认为更好的解决方案(不理想)是在母版页中调用 Html.Action 方法。

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.我正在查看的问题是,如果我在依赖 ViewData 的母版页中放置一个用户控件,我必须为使用所述母版页的每个页面重复包含该 ViewData。

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.如果您只是从基础 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...?因此,因此,如果您正在实施基本 controller,则不使用该特定母版页的操作仍将具有它的视图数据...? (If all that makes sense;-) (如果这一切都有意义的话;-)

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

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