per output dell'istruzione loop che si collegano a controller diversi in asp.net mvc
-
05-07-2019 - |
Domanda
Ho una dichiarazione ciclica nella mia homepage per le notizie ..
Ho questi codici ..
Modello:
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
Controller:
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
Visualizza:
<%@ 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>
l'istruzione for loop genera notizie diverse da controller e viste diversi. Esempio, il primo output potrebbe rendere questa pagina: Community / CommunityNews / 7 il secondo output potrebbe rendere questa pagina: Atletica leggera / Atletica leggera / 5 il terzo output potrebbe rendere questa pagina: Programmi / ProgrammiNews / 2
come farei il codice per il link a quelle pagine? userò javascript? il problema è che non sono così familiare con javascript :( aiuto per favore.. grazie! grazie!
Soluzione
Dovresti essere in grado di generare il secondo argomento per il metodo ActionLink, basato su un campo del tipo di notizie o simile nella tabella. per es.
<%
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})%>
Questo dovrebbe fare il trucco, ma inizierei a preoccuparmi che ci sia troppo codice nella vista. Potrebbe non essere una buona idea passare DataTables come modello, ma a questo punto potrebbe volerci molto lavoro per cambiarlo.
È possibile creare un metodo di supporto che restituirà il controller e l'azione per un determinato tipo di notizia, o meglio ancora, genera un collegamento dato il tipo di notizia. Puoi farlo creando una classe con metodi di estensione per la classe HtmlHelper. Tale metodo sarebbe simile a questo:
<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
Buona fortuna. Penso che ci siano meno persone che usano VB.NET che C # con MVC.
Altri suggerimenti
Suppongo che questa parte del tuo codice di visualizzazione sia dove hai un problema?
Html.ActionLink(dTitle, "__________", New With {id}, DBNull.Value)
Il DBNull.Value
sembra davvero strano. Intendevi Null
?
Ad ogni modo, dovresti essere in grado di usare un sovraccarico come questo:
Html.ActionLink(dTitle, "CommunityNews", "Community", New With {id}, Null)
Non utilizzare JavaScript per questo.