for 循环语句输出链接到 asp.net mvc 中的不同控制器
我的新闻主页中有一个循环语句。
我有这些代码。
模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够根据表中的新闻类型字段或类似字段为 ActionLink 方法生成第二个参数。例如,
这应该可以解决问题,但我会开始担心视图中的代码太多。将 DataTables 作为模型传递可能不是一个好主意,但此时可能需要大量工作来更改它。
您可以创建一个辅助方法,该方法将返回特定新闻类型的控制器和操作,或者更好的是,生成给定新闻类型的链接。您可以通过为 HtmlHelper 类创建一个具有扩展方法的类来实现这一点。该方法看起来类似于:
祝你好运。我认为使用 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.
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:
Good luck. I think there are less people using VB.NET than C# with MVC.
我认为您的视图代码的这一部分是您遇到问题的地方?
DBNull.Value
看起来真的很奇怪。您的意思是Null
吗?无论如何,您应该能够使用这样的重载:
Don't use JavaScript for this。
I assume that this part of your view code is where you have a problem?
The
DBNull.Value
looks really strange. Did you meanNull
?Anyway, you should be able to use an overload like this:
Don't use JavaScript for this.