如何使一个aspx页面,多个内容部分可见的ASP.NET MVC该页上的变量? [英] How to make a variable on an aspx page visible to multiple content sections on that page in ASP.NET MVC?

查看:114
本文介绍了如何使一个aspx页面,多个内容部分可见的ASP.NET MVC该页上的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在ASP.NET MVC应用程序的多个内容部分的aspx页面。我需要在页面上计算基于页面的url值(使用定制的HtmlHelper),并在其中的两个内容页中使用该值。我如何声明这个值(或更正确的变量保存的值),所以它是从两个内容部分可见。

I have an aspx page that has multiple content sections in an ASP.NET MVC application. I need to calculate a value based on the page url (using a custom HtmlHelper) on the page and use that value in two of these content pages. How do I declare this value (or to be more correct the variable holding the value) so it is visible from both content sections.

下面是一些细节:

    <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
        <%
              var titleAndDescription = Helpers.TitleAndDescription.GetTitleAndDescription(Request.RawUrl); 
              Response.Write(titleAndDescription.Title);
          %>

    </asp:Content>

<asp:Content id="indexDescription" ContentPlaceHolderID="MetaTagsContent" runat="server">
    <meta name="Description" content=<%Response.Write(titleAndDescription.Description);%> />
</asp:Content>

所以我想一次计算变量titleAndDescription并在每个部分使用了两次,一次。

So I want to calculate the variable titleAndDescription once and use it twice, once in each partial.

推荐答案

您可以声明那些部分外的变量:

You could declare the variable outside those sections:

<script type="text/c#" runat="server">
    string foo = "bar";
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    <%: foo %>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: foo %></h2>
</asp:Content>

这篇关于如何使一个aspx页面,多个内容部分可见的ASP.NET MVC该页上的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆