ASP.NET MVC의 Different Controllers에 연결되는 루프 명령문 출력
-
05-07-2019 - |
문제
내 홈페이지에 뉴스에 대한 반복 성명서가 있습니다 ..
이 코드가 있습니다 ..
모델 :
Imports Microsoft.VisualBasic
Imports System.Data
Public Class ClassNewsConnection
Inherits ClassConnection
'Featured News for Home Page
Public Function NewsFeatureHome() As DataTable
Return ReadData("SELECT * FROM news WHERE newsFeature = '" & 1 & "' ORDER BY newsID DESC LIMIT 3 ")
End Function
End Class
컨트롤러 :
Public Class HomeController
Inherits Global.System.Web.Mvc.Controller
Private News As New ClassNewsConnection
Private Announcement As New ClassAnnouncementConnection
Private Process As New ClassHTML
Function Index() As ActionResult
Dim dNews As DataTable = News.NewsFeatureHome()
For dCount As Integer = 0 To dNews.Rows.Count - 1
dNews.Rows(dCount).Item("newsTitle") = Process.ToHTML(dNews.Rows(dCount).Item("newsTitle"))
dNews.Rows(dCount).Item("newsContent") = Process.ToHTML(dNews.Rows(dCount).Item("newsContent"))
Next
Return View(dData)
End Function
End Class
보다 :
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/SiteMasterPage.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.Data" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<div>
<label for="News">News</label>
<%Dim dNews As DataTable = ViewData.Model%>
<%Dim id As Integer%>
<%Dim dTitle As String%>
<%For dCount As Integer = 0 To dNews.Rows.Count - 1%>
<%Dim dContent As String = dNews.Rows(dCount).Item("newsContent")%>
<%id = dNews.Rows(dCount).Item("newsID")%>
<p>
<%dTitle = dNews.Rows(dCount).Item("newsTitle")%>
<%=Html.ActionLink(dTitle, "__________", New With {id}, DBNull.Value)%>
<img src='<%=Url.Content("~/NewsImages/" + dNews.Rows(dCount).Item("newsThumbnail")) %>' alt="" />
<%If dContent.Length > 100 Then%>
<%dContent = dContent.Substring(0, dContent.IndexOf("", 300)) & "..."%>
<%Else%>
<%dContent = dContent%>
<%End If%>
<%=Html.ActionLink("Read More", "__________", New With {id}, DBNull.Value)%>
</p>
<%Next%>
</div>
</asp:Content>
for 루프 명령문은 다른 컨트롤러와 뷰와 다른 뉴스를 출력합니다. 예를 들어, 첫 번째 출력은이 페이지를 렌더링 할 수 있습니다 : 커뮤니티/커뮤니티 노출/7 두 번째 출력은이 페이지를 렌더링 할 수 있습니다 : Athletics/Athleticsnews/5 세 번째 출력은이 페이지를 렌더링 할 수 있습니다. 프로그램/프로그램 News/2
해당 페이지 링크의 코드를 어떻게 만드나요? JavaScript를 사용할 수 있습니까? 문제는 JavaScript에 그리 익숙하지 않다는 것입니다 :( 도와주세요 .. 감사합니다! 감사합니다!
해결책
뉴스 유형 필드 또는 테이블에서 유사한 ActionLink 메소드에 대한 두 번째 인수를 생성 할 수 있어야합니다. 예를 들어
<%
Dim newsType As String = dNews.Rows(dCount).Item("newsType")
Dim controllerName As String
Dim actionName as String
' I'm guessing you have a field similar to this:
If (newsType = "Com. News") then
controllerName = "Community"
actionName = "CommunityNews"
End If
If (newsType = "Ath. News") then
controllerName = "Athletics"
actionName = "AthleticsNews"
End If
%>
<%=Html.ActionLink(dTitle, actionName, controllerName, New With {Id = id})%>
이것은 트릭을 수행해야하지만, 나는보기에 너무 많은 코드가있을 것이라고 걱정하기 시작할 것입니다. 모델로 데이터를 전달하는 것은 좋은 생각이 아닐 수도 있지만,이 시점에서이를 변경하는 데 많은 작업이 필요할 수 있습니다.
특정 뉴스 유형에 대한 컨트롤러와 조치를 반환하는 도우미 방법을 만들 수 있습니다. htmlhelper 클래스를위한 확장 메소드가있는 클래스를 만들어서 그렇게 할 수 있습니다. 이 방법은 이것과 유사하게 보일 것입니다.
<Extension()> _
Public Sub NewsLink(ByVal htmlHelper As HtmlHelper, newsType as string, linkText As String, id As int)
Dim action As String
Dim controller As String
'todo: logic to get action and controller names from news type
return htmlHelper.ActionLink(linkText, action, controller, New With {Id = id})
End Sub
행운을 빕니다. MVC가있는 C#보다 vb.net을 사용하는 사람이 적다 고 생각합니다.
다른 팁
뷰 코드 의이 부분이 문제가있는 곳이라고 가정합니다.
Html.ActionLink(dTitle, "__________", New With {id}, DBNull.Value)
그만큼 DBNull.Value
정말 이상해 보입니다. 그런 뜻 이었습니까 Null
?
어쨌든, 당신은 다음과 같은 과부하를 사용할 수 있어야합니다.
Html.ActionLink(dTitle, "CommunityNews", "Community", New With {id}, Null)
이를 위해 JavaScript를 사용하지 마십시오.