I'm very stupid and I can’t understand how to save the result of each loop pass in the array or in the sheet or in the collection, and only then pass it to the View. Accordingly, the controller code:

foreach (var k in db.Sources.Where(c => c.id != null).Select(c => c.URL)) { //какая то логика var RSSFeedData = (from x in xml.Descendants("item") select new RSSFeed { Title = (string)x.Element("title"), Link = (string)x.Element("link"), RSSName = (string)x.Element("Name"), PubDate = (string)x.Element("pubDate") }); ViewBag.RSSFeed = RSSFeedData; } return view(); 

I tried this: ViewBag.RSSFeed = ViewBag.RSSFeed.Add(RSSFeedData); does not work. I tried to create an array, with the string type, but I also could not assign RSSFeed there. with the sheet is the same.

  <tbody> @foreach (var item in ViewBag.RSSFeed) { <tr> <td><a href="@item.Link">@item.Title</a></td> <td>@item.RSSName</td> <td>@item.PubDate</td> </tr> } </tbody> 

    1 answer 1

    You rewrite the ViewBag.RSSFeed in each cycle. By the time the View View is formed, ViewBag.RSSFeed will contain the last entry recorded.

    Make sure that a non-empty value is written to RSSFeedData.

    Example for a list of string (or any other class):

    Controller:

     List<string> Items = new List<string>(); var Sources = new List<int>{1,2,3,4,5}; foreach(var k in Sources) { Items.Add("Template "+ k); } ViewBag.Items = Items; return View(); 

    View:

     <ul> @foreach (var item in ViewBag.Item) { <li>@item</li> } </ul> 
    • var Sources = new List <int> {1,2,3,4,5}; Can you tell what happens in this line? and I already have such a cycle in View, if I change RSSFeed to Items, the compiler swears that it cannot define @foreach (var item in ViewBag.RSSFeed) { <tr> <td><a href="@item.Link">@item.Title</a></td> <td>@item.RSSName</td> <td>@item.PubDate</td> </tr> } elements @foreach (var item in ViewBag.RSSFeed) { <tr> <td><a href="@item.Link">@item.Title</a></td> <td>@item.RSSName</td> <td>@item.PubDate</td> </tr> } - Dmitry Fadin
    • I added the code to the question in a normal form, and also changed the cycle a bit in the controller to show that I am taking some data from the database - Dmitry Fadin
    • var Sources = new List<int>{1,2,3,4,5}; - A list of 5 elements of type int is formed. var RSSFeedData = new List<RSSFeed>(); before the cycle. In the cycle RSSFeedData.Add(); And ViewBag.RSSFeed = RSSFeedData; after the loop before the direct formation of the View. The final step is to dynamically create the RSSFeed property in the ViewBag object. If a property is not created on View, it will not be found and will generate an error. - AlexandrPlas
    • I'm sorry that I’m digging this way, but there are two errors, I 'IEnumerable<RSSFeed>' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'IEnumerable<RSSFeed>' could be found (are you missing a using directive or an assembly reference?) A local or parameter named 'RSSFeedData' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter BDParser 'IEnumerable<RSSFeed>' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'IEnumerable<RSSFeed>' could be found (are you missing a using directive or an assembly reference?) A local or parameter named 'RSSFeedData' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter BDParser
    • 1. Add takes 1 item - you try to cram IEnumiranle, which is an enumeration. Use AddRange or write down on 1. 2. Read the error more carefully, and then rename the variable. - AlexandrPlas