c#使用数据库MVC中的数据创建Rss-Feed
本文关键字:数据 创建 Rss-Feed MVC 数据库 | 更新日期: 2023-09-27 18:27:59
我想在MVC应用程序中使用控制器自动创建Rss-Feed。我必须使用数据库中的数据。我需要的桌子是电影。这是我目前使用的代码:
public ContentResult GenerateRssFeed()
{
string xmlString = "<div>Test</div>";
CineStarEntities entities = new CineStarEntities();
foreach (Movie movie in entities.Movie.Where(m => m.MovieID !=null).ToList()
{
}
return Content(xmlString);`
}
如何编写foreach循环以输出数据?
我找到了答案。您必须将数据库中的属性添加到项目中。守则如下。
List<SyndicationItem> items = new List<SyndicationItem>();
CineStarEntities entities = new CineStarEntities();
foreach (Movie movie in entities.Movie.OrderBy(m => m.MovieTitle))
{
SyndicationItem item = new SyndicationItem();
item.Title = new TextSyndicationContent(movie.MovieTitle);
item.Content = new TextSyndicationContent(movie.Actors);
SyndicationItem item1 = new SyndicationItem();
item1.Content = new TextSyndicationContent(movie.Trailer);
SyndicationItem item2 = new SyndicationItem();
item2.Content = new TextSyndicationContent(movie.Duration.ToString());
SyndicationItem item3 = new SyndicationItem();
item3.Content = new TextSyndicationContent(movie.AgeRelease.ToString());
SyndicationItem item4 = new SyndicationItem();
item4.Content = new TextSyndicationContent(movie.Description);
SyndicationItem item6 = new SyndicationItem();
item6.Content = new TextSyndicationContent(movie.Direction);
SyndicationItem item7 = new SyndicationItem();
item7.Content = new TextSyndicationContent(movie.Direction);
SyndicationItem item8 = new SyndicationItem();
item8.Content = new TextSyndicationContent(movie.Production);
SyndicationItem item9 = new SyndicationItem();
item9.Content = new TextSyndicationContent(movie.Music);
items.Add(item);
items.Add(item1);
items.Add(item2);
items.Add(item3);
items.Add(item4);
items.Add(item6);
items.Add(item7);
items.Add(item8);
items.Add(item9);