Asp.Net的mvc - 如何有一个"控制器"在共享视图 [英] Asp.Net Mvc - How to have a "controller" in shared view

查看:132
本文介绍了Asp.Net的mvc - 如何有一个"控制器"在共享视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在_Layout.cshtml共享视图中为我的名为_Header.cshtml标题。

I have a shared view in my _Layout.cshtml for my header named "_Header.cshtml".

我想显示从数据库中的文本和图像,所以我需要我的控制器去的数据库,并将其返回_Header.cshtml。

I would like to display text and image from the database, so I need my controller to go in the database and return it to the _Header.cshtml.

我怎么能做到这一点,因为所谓的控制器始终是不同的每个页面的用户进入。有没有办法让控制器共享视图?

How can I do that because the controller called is always different each page the user goes. Is there a way to have controller with Shared View?

下面是_Layout.cshtml

Here is the _Layout.cshtml


    

    <div id="header">
        <div id="title">
            @Html.Partial("_Header")
        </div>

        <div id="logindisplay">
           @Html.Partial("_CultureChooser")
            <br />
           @Html.Partial("_LogOnPartial")
        </div>

        <div id="menucontainer">
           @Html.Partial( "_MenuPartial")
        </div>
    </div>

    <div id="main">
        @RenderBody()
        <div id="footer">
        </div>
    </div>

</div>

推荐答案

在你的动作位指示,你可以指定视图的名称:

In your contoller action you could specify the name of the view:

public class MenuController : Controller
{
    [ChildActionOnly]
    public ActionResult Header()
    {
        var model = ... // go to the database and fetch a model
        return View("~/Views/Shared/_Header.cshtml", model);
    }
}

现在在你的 _Layout.cshtml 而不是 @ Html.Partial(_头)的做到这一点:

Now in your _Layout.cshtml instead of @Html.Partial("_Header") do this:

@Html.Action("Header", "Menu")

这篇关于Asp.Net的mvc - 如何有一个&QUOT;控制器&QUOT;在共享视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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