0

我的主页上有一条循环语句以获取新闻..

我有这些代码..

模型 :

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 循环语句从不同的控制器和视图输出不同的新闻。例如,第一个输出可以呈现这个页面:Community/CommunityNews/7 第二个输出可以呈现这个页面:Athletics/AthleticsNews/5 第三个输出可以呈现这个页面:节目/节目新闻/2

我将如何制作链接到这些页面的代码?我会使用 javascript 吗?问题是,我对 javascript 不是很熟悉 :( 请帮助.. 谢谢!谢谢!

4

2 回答 2

1

您应该能够根据表中的新闻类型字段或类似字段为 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})%>

这应该可以解决问题,但我会开始担心视图中的代码过多。将 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 的人比使用 C# 和 MVC 的人少。

于 2009-10-23T06:56:26.077 回答
0

我假设您的视图代码的这一部分是您有问题的地方?

Html.ActionLink(dTitle, "__________", New With {id}, DBNull.Value)

DBNull.Value看起来真的很奇怪。你的意思是Null

无论如何,您应该能够使用这样的重载:

Html.ActionLink(dTitle, "CommunityNews", "Community", New With {id}, Null)

不要为此使用 JavaScript。

于 2009-10-23T06:57:43.983 回答