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.TextSyndicationContentSystem.ServiceModel.Syndication.TextSyndicationContentSystem.ServiceModel.Syndication.NullNotAllowedCollection 1 [
System.ServiceModel.Syndication.SyndicationLink ] System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.TextSyn 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を返します。(RssModelの)IEnumerableを作成し、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 に答える