0

我想在我的视图中输出我的 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.TextSyndicationContent System.ServiceModel.Syndication.TextSyndicationContent System。 ServiceModel.Syndication.NullNotAllowedCollection`1[System.ServiceModel.Syndication.SyndicationLink]1[System.ServiceModel.Syndication.SyndicationLink]
System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.NullNotAllowedCollection



4

2 回答 2

1

Return View(viewModel)返回的是单个 RssModel,而不是 RssModel 列表。您应该创建一个 IEnumerable(of RssModel) 并将其填充到 For Each 循环中,然后将 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)())
于 2012-08-14T13:58:28.927 回答
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
于 2012-08-14T14:51:10.640 回答