for循环语句输出链接到asp.net mvc中的不同控制器
-
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 第三个输出可以呈现此页面:Programs / ProgramsNews / 2
我如何制作链接到这些页面的代码? 我会使用javascript吗?问题是,我不是那么熟悉javascript :( 请帮忙.. 谢谢! 谢谢!
解决方案
您应该能够根据表中的新闻类型字段或类似字段为ActionLink方法生成第二个参数。 e.g。
<%
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})%>
这应该可以解决问题,但我会开始担心视图中的代码太多了。将DataTables作为模型传递可能不是一个好主意,但此时可能需要做很多工作才能改变它。
您可以创建一个帮助方法,该方法将返回某个新闻类型的控制器和操作,或者更好的是,生成给定新闻类型的链接。您可以通过为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
祝你好运。我认为使用VB.NET的人数少于使用MVC的C#。 其他提示
我认为您的视图代码的这一部分是您遇到问题的地方?
Html.ActionLink(dTitle, "__________", New With {id}, DBNull.Value)
DBNull.Value
看起来很奇怪。你的意思是 Null
?
无论如何,您应该可以使用这样的重载:
Html.ActionLink(dTitle, "CommunityNews", "Community", New With {id}, Null)
不要使用JavaScript。
不隶属于 StackOverflow