如何将viewmodel传递到布局/母版页? [英] How to pass viewmodel to a layout/master page?

查看:78
本文介绍了如何将viewmodel传递到布局/母版页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用谷歌搜索已有一段时间,我对如何在asp mvc 3中执行此操作感到有些困惑.

Having googling for some time, I'm a little bit confused with how to do this in asp mvc 3.

因此,任务是为多个控制器的视图提供一个通用的布局(或母版?)页面.所有视图本身都是强类型的...实际上,此布局页面可视化了一些强类型的对象.因此,我需要将此对象传递到布局页面,以将其传递到"Html.RenderPartial()"或直接在页面中呈现.

So, the task is to have a common layout (or master?) page for several controllers' views. All the views are strongly-typed themselves... This layout page in fact visualizes some strongly-typed object. So, I need this object to be passed to the layout page to pass it to `Html.RenderPartial()' or to render it right in the page.

文章将数据传递到View母版页(C#)(尽管mvc2是旧版本)给出了提供基本抽象控制器类的示例,该模型将模型传递给 ViewData .这可能是治疗方法.

The article Passing Data to View Master Pages (C#) (though an old one for mvc2) gives an example to provide a base abstract controller class, passing the model to ViewData. This could be the cure.

但是...

在某些线程中,我发现了一种可以创建强类型母版页的方法:

In some threads I found a way one could make a strongly-typed master page:

<%@ Master ... Inherits="System.Web.Mvc.ViewMasterPage<MyWeb.Models.Client>" %>

那么,在mvc3中有相同的方法吗?还是将数据传递到布局页面的唯一方法是使用ViewBag或ViewData?

So, is there the same approach in mvc3? Or the only way to pass data to a layout page is to use ViewBag or ViewData?

已阅读

Having read what is the best way to bind ViewModel data to master page in asp.net-mvc, I've considered Darin's answer, but still think that this bunch of views will have the same layout, and see no reason to go against DRY principle.

那么,如何将强类型的对象传递到布局页面?还是我把一些东西弄混了?

So, how to pass strongly-typed objects to layout pages? Or have I mixed something up?

推荐答案

这是一个有趣的辩论主题->将视图模型传递给母版页/布局与 ViewBags .

It's an interesting debate topic -> passing view models to the master page / layout vs ViewBags.

我讨厌使用 ViewBags ,所以我拥有所有视图层的视图模型.从 _layout.cshtml 开始.任何使用Layout的视图都可以继承该Layout或它下面的任何视图,然后重复该操作,直到到达最低级别,通常是_layout ...

I'm -hate- using ViewBags so I have view models for all my view-layers. Starting with the _layout.cshtml and upwards. Any view that uses a Layout .. well .. that view model just inherits the Layout .. or whatever view is below it .. and this is repeated until you hit the bottom level which is usually the _layout...

我的 RavenOverflow项目包含一些示例代码来显示这一点.

My RavenOverflow project has some sample code that shows this.

  • This is the the layout view, defined.
  • Here is the layout view model, defined.
  • This is the view that uses a layout.
  • Here is the view model, for that previous view.
  • This is some controller code, that creates the model .. which is then passed into the View.

这篇关于如何将viewmodel传递到布局/母版页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆