For the task I have to download several items from several xml documents (RssReader) then display random 4 elements on the page. I have already done all the code, the only problem is that the page displays the same 4 items, if I set the trap in the debuggeer in the view/controller everything loads correctly. I do not know what the problem may be, it's my beginnings in ASP.NET for all the clues to the code, thank you very much!
View code:
<table class="table">
@foreach (var item in Model)
{
<tr>
<td>
@item.Title
</td>
<td>
@item.PubDate
</td>
</tr>
}
</table>
Model Code:
public class RssItem
{
public string Title { get; set; }
public string PubDate { get; set; }
}
Controller Code:
public class PortfolioController : Controller
{
// GET: Portfolio
public ActionResult Index()
{
var linkList = new List<string> {
"https://news.google.com/rss?hl=pl&gl=PL&ceid=PL:pl",
"https://news.google.com/rss?hl=pl&gl=PL&ceid=PL:pl",
"https://news.google.com/rss?hl=pl&gl=PL&ceid=PL:pl",
"https://news.google.com/rss?hl=pl&gl=PL&ceid=PL:pl"
};
List<RssItem> rssItems = new List<RssItem>();
List<RssItem> randomRSS = new List<RssItem>();
foreach (string linkRss in linkList)
{
XElement xml = XElement.Load(linkRss);
var query = xml.Descendants("item").Select(item =>
new RssItem
{
Title = item.Element("title").Value,
PubDate = item.Element("pubDate").Value,
}).ToList();
foreach (var item in query)
{
rssItems.Add(item);
}
}
for (int i = 0; i < 4; i++)
{
Random random = new Random();
int randomInt = random.Next(rssItems.Count);
randomRSS.Add(rssItems[randomInt]);
}
return View(randomRSS);
}
}