我能在我的网站上继承一些东西吗?主页MVC吗?

时间:2022-02-27 00:35:11

I am trying to familiarize myself with the MVC environment. I would like to add a side menu on my default master page. I however need to add an inherit to get my data. Can I do that or I do I have to keep the 'Inherits="System.Web.Mvc.ViewMasterPage"'?

我正在尝试熟悉MVC环境。我想在我的默认主页上添加一个边菜单。但是,我需要添加一个继承来获取我的数据。我可以这样做吗?还是我必须保留'Inherits= ' System.Web.Mvc.ViewMasterPage ' '?

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<body>

<div class="page">
    <div id="header">
        <div id="title">
            <h1>My MVC Application</h1>
        </div>

        <div id="logindisplay">
            <% Html.RenderPartial("LogOnUserControl"); %>
        </div> 

        <div id="menucontainer">

            <ul id="menu">              
                <li><%= Html.ActionLink("Home", "Index", "Home")%></li>                    
                <li><%= Html.ActionLink("Summary", "Summary", "Home")%></li>                    
            </ul>

        </div>
    </div>

    <div id="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server">
        <div id="categories">
            <fieldset> 

                <% Html.RenderPartial("SideMenu", new ViewPage<DataLibrary.MenuOptions>().Model); %>
<!--I want to use ("SideMenu", Model) above -->
            </fieldset>
        </div>            
        </asp:ContentPlaceHolder>
        <div id="footer">
        </div>
    </div>
</div>

2 个解决方案

#1


2  

You don't need to Inherit, I believe you just need to use an import directive

你不需要继承,我相信你只需要使用导入指令

such as

<%@ Import Namespace="DataLibrary" %>

Also, I think the line

而且,我认为这条线

<% Html.RenderPartial("SideMenu", 
                      new ViewPage<DataLibrary.MenuOptions>().Model); %>

should be

应该是

<% Html.RenderPartial("SideMenu", new MenuOptions()); %>

Kindness,

善良,

Dan

#2


0  

You can inherit all your controllers from a main controller.

您可以从主控制器继承所有的控制器。

Then, in your MainController you can set all the data needed in the master page (all the global things)

然后,在主控制器中,您可以设置主页中所需的所有数据(所有全局内容)

Public MustInherit Class ApplicationController
    Inherits System.Web.Mvc.Controller

    Sub New()
        ViewData("SideMenu") = getSideMenu()
    End Sub

End Class

Later, in the Master page

稍后,在母版页面中

<% Html.RenderPartial("SideMenu", ViewData("SideMenu")); %>

There are other ways to do it (this is just the way I do it), and in MVC 2.0 there is a new option to refer to a controller in the partial.

还有其他的方法(我就是这么做的),在MVC 2.0中有一个新的选项可以引用部分中的控制器。

#1


2  

You don't need to Inherit, I believe you just need to use an import directive

你不需要继承,我相信你只需要使用导入指令

such as

<%@ Import Namespace="DataLibrary" %>

Also, I think the line

而且,我认为这条线

<% Html.RenderPartial("SideMenu", 
                      new ViewPage<DataLibrary.MenuOptions>().Model); %>

should be

应该是

<% Html.RenderPartial("SideMenu", new MenuOptions()); %>

Kindness,

善良,

Dan

#2


0  

You can inherit all your controllers from a main controller.

您可以从主控制器继承所有的控制器。

Then, in your MainController you can set all the data needed in the master page (all the global things)

然后,在主控制器中,您可以设置主页中所需的所有数据(所有全局内容)

Public MustInherit Class ApplicationController
    Inherits System.Web.Mvc.Controller

    Sub New()
        ViewData("SideMenu") = getSideMenu()
    End Sub

End Class

Later, in the Master page

稍后,在母版页面中

<% Html.RenderPartial("SideMenu", ViewData("SideMenu")); %>

There are other ways to do it (this is just the way I do it), and in MVC 2.0 there is a new option to refer to a controller in the partial.

还有其他的方法(我就是这么做的),在MVC 2.0中有一个新的选项可以引用部分中的控制器。