将模型传递到MS MVC中的布局 [英] Passing model to layout in MS MVC

查看:76
本文介绍了将模型传递到MS MVC中的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图传递模型中,我的输入方式很严格,如下所示:

In views i`m passing model strongly typed way like this:

System.Web.Mvc.ViewPage<HomeModel>

然后使用它:

<%= Model.Greeting %>

在布局中如何使用强类型模型?

How it would be possible to use strongly typed model in layout?

如果没有强类型输入,我可能会在控制器工厂添加必要的数据,然后通过(LayoutModel)Viewdata ["LayoutModel"]).Tralala使用它,但我正在寻找更好的方法.

Without strongly typing i would probably add necessary data at controller factory, then use it through (LayoutModel)Viewdata["LayoutModel"]).Tralala, but i`m looking for better way.

推荐答案

在视图中创建强类型属性:

Create strongly typed property in your View:

<script runat="server">
    protected LayoutModel LayoutModel
    {
        get
        {
            return ViewData["LayoutModel"] as LayoutModel;
        }
    }
</script>

这篇关于将模型传递到MS MVC中的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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