for 循环语句输出链接到 asp.net mvc 中的不同控制器

发布于 2024-08-08 21:04:20 字数 2899 浏览 7 评论 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 第三个输出可以呈现此页面:Programs/ProgramsNews/2

我将如何为这些页面的链接编写代码? 我会使用 javascript 吗?问题是,我对 javascript 不太熟悉:( 请帮忙.. 谢谢你! 谢谢你!

i have a looping statement in in my homepage for news..

I have these codes..

Model :

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

View :

<%@ 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>

the for loop statement outputs different news from different controllers and views..
Example,
the first output could render this page : Community/CommunityNews/7
the second output could render this page : Athletics/AthleticsNews/5
the third output could render this page : Programs/ProgramsNews/2

how would i make the code for the link to those pages?
will i use javascript?the problem is, i'm not that so familiar with javascript :(
help please..
thank you!
thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帥小哥 2024-08-15 21:04:20

您应该能够根据表中的新闻类型字段或类似字段为 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 的人比使用 MVC 的 C# 的人少。

You should be able to generate the second argument for the ActionLink method, based on a news type field or similar in your table. 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})%>

This should do the trick, but I would start to worry that there is getting to be too much code in the view. It might not be a good idea to pass DataTables in as your model, but it could take a lot of work to change that at this point.

You could create a helper method that will return the controller and action for a certain news type, or better yet, generate a link given the news type. You can do that by creating a class with extension methods for the HtmlHelper class. That method would look similar to this:

<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

Good luck. I think there are less people using VB.NET than C# with MVC.

话少情深 2024-08-15 21:04:20

我认为您的视图代码的这一部分是您遇到问题的地方?

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

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

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

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

Don't use JavaScript for this。

I assume that this part of your view code is where you have a problem?

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

The DBNull.Value looks really strange. Did you mean Null?

Anyway, you should be able to use an overload like this:

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

Don't use JavaScript for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文