for loop statement outputs that links to differnent controllers in asp.net mvc
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开发者_StackOverflow社区 those pages? will i use javascript?the problem is, i'm not that so familiar with javascript :( help please.. thank you! thank you!
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.
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.
精彩评论