1

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);
    }
}

Without traps in debugger: Without traps in debugger

With traps in view and controller (debugger mode): enter image description here

VDWWD
  • 35,079
  • 22
  • 62
  • 79
Danki
  • 95
  • 2
  • 9

1 Answers1

1

Why do four requests? You can take the items from a single request and display random 4. But in order for Random to work it needs to be outside the for loop.

Random random = new Random();

for (int i = 0; i < 4; i++)
{ 
    int randomInt = random.Next(rssItems.Count);
    randomRSS.Add(rssItems[randomInt]);
}

But you could still end up with duplicates if you do not remove the rss from the list since random can create duplicates, see this answer for solutions Random number generator with no duplicates

VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Placing random outside the loop solves the problem! thank you very much! Following the advice I will make a code without 4 requests – Danki Mar 11 '19 at 20:21