2012-08-14 26 views
0

我想输出在我看来,我的RSS提要这样的:如何通过类型SyndicationFeed循环输出RSS以查看?

@ModelType IEnumerable(Of MyBlog.RssModel) 

<table> 
    <tr> 
     <th> 
      Title 
     </th> 
     <th> 
      Description 
     </th> 
     <th> 
      Link 
     </th> 
     <th></th> 
    </tr> 

@For Each item In Model 
    Dim currentItem = item 
    @<tr> 
     <td> 
      @Html.DisplayFor(Function(modelItem) currentItem.Title) 
     </td> 
     <td> 
      @Html.DisplayFor(Function(modelItem) currentItem.Description) 
     </td> 
     <td> 
      @Html.DisplayFor(Function(modelItem) currentItem.Link) 
     </td> 
     <td> 
     </td> 
    </tr> 
Next 

</table> 

这里是我的代码:

Function ShowFeed() As ActionResult 

    Dim feedUrl = "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml" 
    Dim feed As SyndicationFeed = GetFeed(feedUrl) 

    Dim model As IList(Of RssModel) = New List(Of RssModel)() 

    For Each item As SyndicationItem In feed.Items 
     Dim rss As New RssModel() 
     rss.Title = item.Title.ToString 
     rss.Description = item.Summary.ToString 
     rss.Link = item.Links.ToString 

     model.Add(rss) 
    Next 

    Return View(model) 

End Function 

,则会产生意外结果:

标题描述链接
系统.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.TextSyndi cationContent
System.ServiceModel.Syndication.NullNotAllowedCollection 1[System.ServiceModel.Syndication.SyndicationLink]
System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.NullNotAllowedCollection
1 [System.ServiceModel.Syndication.SyndicationLink]
System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.NullNotAllowedCollection`1 [System.ServiceModel.Syndication.SyndicationLink]

回答

0

答案是这样的:

Function ShowFeed() As ActionResult 

     Dim feedUrl = "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml" 
     Dim feed As SyndicationFeed = GetFeed(feedUrl) 

     Dim model As IList(Of RssModel) = New List(Of RssModel)() 

     For Each item As SyndicationItem In feed.Items 
      Dim rss As New RssModel() 
      rss.Title = item.Title.Text 
      rss.Description = item.Summary.Text 
      rss.Link = item.Links.First.Uri.ToString 

      model.Add(rss) 
     Next 

     Return View(model) 

    End Function 
1

您的Return View(viewModel)正在返回一个RssModel,而不是一个RssModel列表。您应该创建一个IEnumerable(的RssModel)并将其填充到For Each循环中,然后将IEnumerable返回给View。

编辑:从c#使用代码转换器到VB,但这应该告诉你进步。

Dim model As IList(Of RssModel) = New List(Of RssModel)() 

For Each item As var In feed 
    Dim rss As New RssModel() 
    rss.Something = item.Something 

    model.Add(rss) 
Next 

Return View(model.AsEnumerable(Of RssModel)()) 
+0

谢谢,罗布。你能告诉我一个如何创建IEnumberable(RSSModel)并将其填充到For Each循环中的例子吗? – user1477388 2012-08-14 14:00:00

+1

我在上面的答案中添加了一些代码示例。 – robasta 2012-08-14 14:08:33

+0

我在“feed”上得到了同样的错误,它说我无法在其上使用foreach循环“错误表达式的类型为'System.ServiceModel.Syndication.SyndicationFeed',它不是一个集合类型。 – user1477388 2012-08-14 14:11:53