2013-10-23 64 views
3

这是我的第一篇文章。 所以我有这个问题,我对这种语言或c#很新。阅读rss饲料与c#mvc4

我有一个读取新闻rss的模型,然后使用相同的索引控制器,我必须将它传递给视图。

这是我的模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Xml.Linq; 

namespace Fantacalcio.Web.Areas.Admin.Models 
{ 
    public class FeedGazzetta 
    { 
     public string Title { get; set; } 
     public string Description { get; set; } 
     public string Link { get; set; } 
     public string PubDate { get; set; } 
     public string Image { get; set; } 
    } 

    public class ReadFeedGazzetta 
    { 
     public static List<FeedGazzetta> GetFeed() 
     { 
      var client = new WebClient(); 
      client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 
      var xmlData = client.DownloadString("http://www.gazzetta.it/rss/Calcio.xml"); 

      XDocument xml = XDocument.Parse(xmlData); 

      var GazzettaUpdates = (from story in xml.Descendants("item") 
          select new FeedGazzetta 
          { 
           Title = ((string)story.Element("title")), 
           Link = ((string)story.Element("link")), 
           Description = ((string)story.Element("description")), 
           PubDate = ((string)story.Element("pubDate")), 
           Image = ((string)story.Element("enclosure").Attribute("url")) 
          }).Take(10).ToList(); 

      return GazzettaUpdates; 
     } 
    } 

} 

我的控制器如下:

public ActionResult Index() 
     { 

      IndexAdminVm model = new IndexAdminVm(); 

      //List<FeedGazzetta> ListaNotizie = new List<FeedGazzetta>(); 
      model.ListaNotizie = ReadFeedGazzetta.GetFeed(); 
      return View(model); 
     } 

我的视图模型是这样的:

public class IndexAdminVm 
    { 
     public List<FeedGazzetta> ListaNotizie { get; set; } 
    } 

而我的看法是这样的:

@model List<Fantacalcio.Web.Areas.Admin.Models.IndexAdminVm> 


@{ 
    ViewBag.Title = "Home"; 
} 

<h2>Home</h2> 

@foreach (var item in Model) 
{ 
    @item.ListaNotizie.FirstOrDefault().Title <br /> 
    @Html.Raw(item.ListaNotizie.FirstOrDefault().Description) <br /> 
    @item.ListaNotizie.FirstOrDefault().Image <br /> 
    @Convert.ToDateTime(item.ListaNotizie.FirstOrDefault().PubDate) <br /> 
    @item.ListaNotizie.FirstOrDefault().Link <br /> 
    <br /><br /> 
} 

在编制没有得到任何错误,但是当我查看网站上,我得到这个从视图:

传递到字典的模型产品类型Fantacalcio.Web.Areas.Admin的”。 Models.IndexAdminVm',但字典需要一个类型为“System.Collections.Generic.List`1 [Fantacalcio.Web。 Areas.Admin.Models.IndexAdminVm]'。

出了什么问题?

我希望我是清楚的:) 感谢

回答

3

你传递错误的模型查看。 您传递单个IndexAdminVm,但期望此视图模型的列表。您应该将视图改成这样:

@model Fantacalcio.Web.Areas.Admin.Models.IndexAdminVm 

... 

@foreach (var item in Model.ListaNotizie) 

... 
+0

谢谢你这么多 我已经失去了至少一个小时,这件事情。 不幸的是,仍然有这种语言 再次感谢 – enzo