How do I inject plain text into an asp.net MVC1 master page from a child?
All the searches I've come up with talk about regular asp.net where there is a code behind page or you are accessing an asp.net control of some sort.
Since there is no code behind in the master page for Asp.net MVC how would I put a sub page name in plain text/html just under the master page title?
<div id="header">
<div id="menu">
<ul id="main">
<li class="current_page_item" id="menu"><%= Html.ActionLink("Home", "Index", "Home")%></li>
<%-- <li><%= Html.A开发者_运维百科ctionLink("About", "About", "Home")%></li>--%>
</ul>
</div>
<div id="logo">
<h1><span>Defect Severity Assessment Tool</span></h1>
<p id="subpage"><%--*What goes here?*--%></p>
</div>
</div>
<div id="content">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<div id="footer">
</div>
</div>
Use a content placeholder.
Master Page
<div id="header">
<div id="menu">
<ul id="main">
<li class="current_page_item" id="menu"><%= Html.ActionLink("Home", "Index", "Home")%></li>
<%-- <li><%= Html.ActionLink("About", "About", "Home")%></li>--%>
</ul>
</div>
<div id="logo">
<h1><span>Defect Severity Assessment Tool</span></h1>
<p id="subpage"><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></p>
</div>
</div>
<div id="content">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<div id="footer">
</div>
</div>
Content Page
<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
<!-- Your markup here -->
</asp:Content>
You could put the logic that sets the subpage name in a base, abstract controller class that your concrete controller classes derive from. Override OnActionExecuting
in your base controller and add code that looks something like this:
protected override void OnActionExecuting(ActionExecutingContext context) {
ViewData["subpage"] = <add code to set subpage>;
base.OnActionExecuting(context);
}
In your view, you can now reference the subpage name like so:
<div id="logo">
<h1><span>Defect Severity Assessment Tool</span></h1>
<p id="subpage"><% =ViewData["subpage"] %></p>
</div>
If you need to set the value of subpage specifically per controller, simply override OnActionExecuting
in your concrete controller and set the ViewData value there instead.
EDIT:
If the value of subpage is always dependent on the controller currently handling the request, you may be better off defining an abstract method to set its value in the base controller and then overriding it in each concrete controller. For instance:
public abstract class BaseController : Controller
{
protected abstract void SetSubPage();
}
public class ConcreteController : BaseController
{
protected override void SetSubPage()
{
ViewData["subpage"] = <code goes here>;
}
}
Since there is no code behind in the master page for Asp.net MVC
But you can add one.
how would I put a sub page name in plain text/html just under the master page title?
<p id="subpage"><%--*What goes here?*--%></p>
What about this MasterPage:
<p id="subpage"><%= Page.Title %></p>
And your page would be:
<%@ Page Title="Name of Subpage" Language="C#" MasterPageFile="~/Templates/Main.Master" Inherits="System.Web.Mvc.ViewPage" %>
So, we just use Page's Title to render its name on the page.
you could try Html.RenderPartial and render a partial ascx control
精彩评论