Search code examples
asp.netasp.net-mvc-2using-directivesusing-declaration

How to use 'Using' keyword in ASP.NET page without code behind


I want to include some namespaces with their classes in my asp.net application. It is possible with using keyword ?

I have this:

<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>

I used MVC2 architecture ...

I want to use directly the class NewsModel getNewsDetail = ViewData["NewsDetail"] as NewsModel.

Thank you and sorry if my question is poor.


Solution

  • Use Import at the top of the view:

    <%@ Import Namespace="SomeNamespace" %>