【问题标题】:How to use 'Using' keyword in ASP.NET page without code behind如何在没有代码的情况下在 ASP.NET 页面中使用“使用”关键字
【发布时间】:2012-01-16 13:33:06
【问题描述】:

我想在我的 asp.net 应用程序中包含一些名称空间及其类。 using 关键字可以吗?

我有这个:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <%
       SomeNamespace.Models.NewsModel getNewsDetail = ViewData["NewsDetail"] as SomeNamespace.Models.NewsModel;

       if ( getNewsDetail != null ) {
            %>
            <h2>'<%= getNewsDetail.NewsTitle%>' details</h2>
            <div class="discipline-details">
            <ul>
            <li>
            <h3> <%= getNewsDetail.NewsTitle%></h3>            
            <p><%= getNewsDetail.NewsDescription%></p>
            </li>
            </ul>
            </div>
    <%
          } else {
         %> <p class="error">No news is available. Below is listed the available news.</p>
         <div id="home-news">

        </div>
          <%
          }
     %>

</asp:Content>

我使用 MVC2 架构 ...

我想直接使用NewsModel getNewsDetail = ViewData["NewsDetail"] as NewsModel这个类。

谢谢你,如果我的问题很糟糕,请见谅。

【问题讨论】:

  • 视图中的逻辑过多,您应该为此创建一个自定义 HTML 帮助程序。
  • 在你看来,我完全同意 Reigo Hein 的观点......
  • @Reigo:如何创建自己的助手以及在哪里?我必须创建一个具有 TagBuilder 方法的新类?或如何?
  • 是的,您必须创建一个 TagBuilder,它接受您定义的 HTMLHelper 属性和参数。

标签: asp.net asp.net-mvc-2 using-directives using-declaration


【解决方案1】:

根据你的代码块,请你试试下面:(把这段代码写在页面顶部,页面Page指令)

<%@ Import Namespace="SomeNamespace.Models" %>

【讨论】:

    【解决方案2】:

    你可以这样做:

    <%@ Import Namespace="MyNamespace" %>
    

    【讨论】:

      【解决方案3】:

      你可以添加

      <%@ Import Namespace="The.Namespace" %>
      

      在视图的顶部,但最初的想法似乎有问题。您可能应该使用强类型视图。

      【讨论】:

        【解决方案4】:

        在视图顶部使用Import

        <%@ Import Namespace="SomeNamespace" %>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-09
          • 2015-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多