How to use 'Using' keyword in ASP.NET page without code behind
-
12-03-2021 - |
Pregunta
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.
Solución
Use Import
at the top of the view:
<%@ Import Namespace="SomeNamespace" %>
Otros consejos
You can add
<%@ Import Namespace="The.Namespace" %>
at the top of the view but there seems to be something wrong with the original idea. You should probably use strongly typed view.
you can do this:
<%@ Import Namespace="MyNamespace" %>
According to your code block, Would you please try below: (write this code at the top of the page, page Page
directive )
<%@ Import Namespace="SomeNamespace.Models" %>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow