我想以如下方式输出我的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产生意想不到的结果:
标题描述链接 System.ServiceModel.Syndication.TextSyndicationContent System.ServiceModel.Syndication.TextSyndicationContent System.ServiceModel.Syndication.NullNotAllowedCollection
1[System.ServiceModel.Syndication.SyndicationLink]System.ServiceModel.Syndication.TextSyndicationContentSystem.ServiceModel.Syndication.TextSyndicationContentSystem.ServiceModel.Syndication.NullNotAllowedCollection1System.ServiceModel.Syndication.SyndicationLink System.ServiceModel.Syndication.TextSyndicationContent System.ServiceModel.Syndication.TextSyndicationContent System.ServiceModel.Syndication.NullNotAllowedCollection`1System.ServiceModel.Syndication.SyndicationLink
发布于 2012-08-14 14:51:10
答案是:
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发布于 2012-08-14 13:58:28
您的Return View(viewModel)正在返回一个RssModel,而不是一个RssModels列表。您应该创建一个IEnumerable(of RssModel),并在每个循环中填充它,然后将IEnumerable返回到视图。
编辑:使用代码转换器从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)())https://stackoverflow.com/questions/11953847
复制相似问题