如何从剃刀页面模型传递给它的主人/页面布局? [英] How to pass the model from the razor page to its master/layout page?

查看:123
本文介绍了如何从剃刀页面模型传递给它的主人/页面布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页下面顶部:

I have a page with following on top:

@model AppMainModel
@{
    ViewData["Title"] = "Home Page";
}

我想用AppMainModel及其属性在其主/布局页面。如果可能的强类型(例如,避免的ViewData等)。

I would like to use AppMainModel and its properties in its master/layout page. Strongly typed if possible (e.g. avoid ViewData and the like).

这可能吗?

推荐答案

要避免被非强类型的_layout页面上,我建议你看看下面的伟大@的 jgauffin 的博客张贴有关获取信息到布局不使用ViewBag。

To avoid being non-strongly typed on the _Layout page, i suggest you to take a look at the following great @jgauffin blog post about Getting information into the Layout without using ViewBag.

在总结使用LayoutViewModel:


  • 创建所有你需要的_layout页面上的类型/字段视图模型

  • 包裹处理这个LayoutViewModel成BaseController类(即你的其他控制器继承)覆盖了 OnResultExecuting()

  • 创建一个Viewbase布局类将继承 WebViewPage ,而在的web.config 来使用你的Viewbase注册它布局模型为 pageBaseType

  • Create a viewmodel with all the types/fields you need on your _Layout page
  • Wrap handling of this LayoutViewModel into a BaseController class (that your other controllers inherit from) overriding its OnResultExecuting() method
  • Create a Viewbase layout class that will inherit WebViewPage, and register it in web.config to use your Viewbase layout model as pageBaseType

举个例子:我用这个方法来元的字段值传递给我的_layout页

An example: I use this approach to pass meta field values to my _Layout page.

这篇关于如何从剃刀页面模型传递给它的主人/页面布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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