views:

128

answers:

4

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.ActionLink("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>
A: 

you could try Html.RenderPartial and render a partial ascx control

Anthony Shaw
Is this efficient for 2 words of text?
Maslow
In one word? NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNO
surdipkrishna
wow... does that really denote a down vote tho? It was a simple suggestion from my knowledge of aspnet mvc. Thanks SO :-\
Anthony Shaw
+1  A: 

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>;
    }
}
pmarflee
then in each controller that calls this view I have to set this variable?
Maslow
@Maslow: If the value of subpage will differ depending on which controller is handling the request, then yes. Otherwise, you can set the value in the base controller.
pmarflee
+2  A: 

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>
Pawel Krakowiak
This is the method I tend to use in this situation too, I think resorting to ViewData or partial views can be overkill for something as simple as this. A similar approach is taken for the title tag in the MVC default template.
sighohwell
Do you know why if a contentPlaceholder is placed in the same `<p></p>` with regular text, the regular text gets deleted?
Maslow
@Maslow: No, have you tried putting the text in a span element instead?
Pawel Krakowiak
A: 

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.

Dmytrii Nagirniak