Why can't I share a variable between two content sections in an ASP.NET MVC View?

Posted by Dave Van den Eynde on Stack Overflow See other posts from Stack Overflow or by Dave Van den Eynde
Published on 2010-06-09T08:53:50Z Indexed on 2010/06/09 9:12 UTC
Read the original article Hit count: 334

Filed under:

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?

© Stack Overflow or respective owner

Related posts about asp.net-mvc