Reputation: 369
I am not sure how to do this in MVC, in normal code I would just iteratre in my loop and write the item, but I am lost doing this in MVC. In this program I am reading RSS feed from World of Warcraft site. I've got this method:
public ActionResult Bar()
{
List<string> temat = new List<string>();
List<string> podpis = new List<string>();
List<DateTime> czas = new List<DateTime>();
string url = "http://eu.battle.net/wow/en/feed/news";
using (XmlReader reader = XmlReader.Create(url))
{
SyndicationFeed feed = SyndicationFeed.Load(reader);
foreach (SyndicationItem item in feed.Items)
{
String subject = item.Title.Text;
String summary = item.Summary.Text;
DateTime date = item.PublishDate.DateTime;
temat.Add(subject);
podpis.Add(summary);
czas.Add(czasy);
}
}
reader.Close();
ViewBag.tytul = temat;
ViewBag.opis = podpis;
ViewBag.c = czas;
return PartialView("_Bar");
}
And in my partialView I can get to each item from ViewBag like this:
<div class="row">
@foreach (var o in ViewBag.opis)
{
<p>@Html.Raw(@o)</p>
}
</div>
But I need to iterate like this:
"item1" from "list1" => "item1" from "list2" => "item1" from "list3" => "item2" from "list1" => ETC...
I don't know how to do this in this View, can someone help me?
Upvotes: 1
Views: 2058
Reputation: 1661
So, You need to create a class for it because Your approach makes no sense. Let's create a class:
public class MyModel
{
public string Topic { get; set; }
public string Signature { get; set; }
public DateTime Time { get; set; }
}
Later on let's change Your Controller to this:
public ActionResult Bar()
{
List<MyModel> _model = new List<MyModel>();
using (XmlReader reader = XmlReader.Create(url))
{
SyndicationFeed feed = SyndicationFeed.Load(reader);
foreach (SyndicationItem item in feed.Items)
{
var newItem = new MyModel();
newItem.Topic = item.Title.Text;
newItem.Signature = item.Summary.Text;
newItem.Time = item.PublishDate.DateTime;
_model.Add(newItem);
}
}
return PartialView("_Bar", _model);
}
Now You have strongly typed model in Your View so just add a namespace on top(lets assume MyModel is in the xxx.Models namespace):
@model IEnumerable <xxx.Models.MyModel>
Now You can iterate throughout this with intellisense liek this:
@foreach (var item in Model)
{
<p>item.Topic</p>
<p>item.Signature</p>
<p>item.Time</p>
}
Upvotes: 1
Reputation: 526
First of all, build a new class that will hold your data.
Class WOWClass
{
public string temat {get;set;}
public DateTime czas {get;set;}
}
Then populdate a list of WOWClass instead of 3 lists...
public ActionResult Bar()
{
List<WOWClass> list = new List<WOWClass>();
string url = "http://eu.battle.net/wow/en/feed/news";
using (XmlReader reader = XmlReader.Create(url))
{
SyndicationFeed feed = SyndicationFeed.Load(reader);
foreach (SyndicationItem item in feed.Items)
{
//populate your list
}
}
return PartialView("_Bar", list);
}
And finally this is the view:
<div class="row">
@foreach (var o in Model)
{
Temat: <p>@Html.Raw(o.temat)</p>
Czas: <p>@Html.Raw(o.czas)</p>
}
</div>
Upvotes: 0