I have an ASP.NET MVC View with the typical TitleContent and MainContent, with a fairly complicated title that I want to calculate once and then share between these two content sections, like so:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%
var complicatedTitle = string.Format("{0} - {1}", Model.FirstThing, Model.SecondThing);
%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%: complicatedTitle %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: complicatedTitle %></h2>
</asp:Content>
This, however, doesn’t work, as the resulting error message would say that only Content controls are allowed directly in a content page that contains Content controls.
The calculation definately belongs in the view. How do you solve this problem?
Another way is by adding an HTML helper specifically to produce the well-formatted title.
and then invoke it with