Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC中迭代列表中的多个项#_C#_Asp.net Mvc_Asp.net Mvc 4_Foreach_Partial Views - Fatal编程技术网

C# 在MVC中迭代列表中的多个项#

C# 在MVC中迭代列表中的多个项#,c#,asp.net-mvc,asp.net-mvc-4,foreach,partial-views,C#,Asp.net Mvc,Asp.net Mvc 4,Foreach,Partial Views,我不知道如何在MVC中做到这一点,在普通代码中,我只是在循环中迭代并编写项目,但我在MVC中做不到这一点。在这个节目中,我正在阅读来自魔兽世界网站的RSS提要。 我有这个方法: public ActionResult Bar() { List<string> temat = new List<string>(); List<string> podpis = new List<string>(); List<DateTi

我不知道如何在MVC中做到这一点,在普通代码中,我只是在循环中迭代并编写项目,但我在MVC中做不到这一点。在这个节目中,我正在阅读来自魔兽世界网站的RSS提要。 我有这个方法:

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");
}
公共操作结果栏()
{
List temat=新列表();
List podpis=新列表();
List czas=新列表();
字符串url=”http://eu.battle.net/wow/en/feed/news";
使用(XmlReader=XmlReader.Create(url))
{
SyndicationFeed=SyndicationFeed.Load(读卡器);
foreach(feed.Items中的SyndicationItem项目)
{
字符串主题=item.Title.Text;
字符串摘要=item.summary.Text;
DateTime日期=item.PublishDate.DateTime;
temat.Add(主题);
新增(摘要);
czas.Add(czasy);
}
}
reader.Close();
ViewBag.tytul=temat;
ViewBag.opis=podpis;
ViewBag.c=czas;
返回部分视图(“_条”);
}
在我的partialView中,我可以从ViewBag中访问每一项,如下所示:

<div class="row">
    @foreach (var o in ViewBag.opis)
    {
        <p>@Html.Raw(@o)</p>
    }
</div>

@foreach(ViewBag.opis中的变量o)
{
@Html.Raw(@o)

}
但我需要这样迭代:

<div class="row">
    @foreach (var o in ViewBag.opis)
    {
        <p>@Html.Raw(@o)</p>
    }
</div>
“列表1”中的“项目1=>”列表2中的“项目1=>”列表3中的“项目1” =>“列表1”中的“项目2”=>等


我不知道如何在这个视图中执行此操作,有人可以帮助我吗?

首先,构建一个新类来保存您的数据

Class WOWClass
{
    public string temat {get;set;}
    public DateTime czas {get;set;}
}
然后创建一个WOWClass列表,而不是3个列表

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);
}
公共操作结果栏()
{
列表=新列表();
字符串url=”http://eu.battle.net/wow/en/feed/news";
使用(XmlReader=XmlReader.Create(url))
{
SyndicationFeed=SyndicationFeed.Load(读卡器);
foreach(feed.Items中的SyndicationItem项目)
{
//填充您的列表
}
}
返回PartialView(“_栏”,列表);
}
最后,这是一种观点:

<div class="row">
    @foreach (var o in Model)
    {
        Temat: <p>@Html.Raw(o.temat)</p>
        Czas: <p>@Html.Raw(o.czas)</p>
    }
</div>

@foreach(模型中的var o)
{
Temat:@Html.Raw(o.Temat)

Czas:@Html.Raw(o.Czas)

}
因此,您需要为它创建一个类,因为您的方法没有意义。 让我们创建一个类:

public class MyModel
{
  public string Topic { get; set; }
  public string Signature { get; set; }
  public DateTime Time { get; set; }
}
稍后,我们将控制器更改为:

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); 
}
公共操作结果栏()
{
列表_model=新列表();
使用(XmlReader=XmlReader.Create(url))
{
SyndicationFeed=SyndicationFeed.Load(读卡器);
foreach(feed.Items中的SyndicationItem项目)
{
var newItem=new MyModel();
newItem.Topic=item.Title.Text;
newItem.Signature=item.Summary.Text;
newItem.Time=item.PublishDate.DateTime;
_model.Add(newItem);
}
}
返回部分视图(“\u条,\u模型”);
}
现在视图中有了强类型模型,所以只需在顶部添加一个名称空间(假设MyModel位于xxx.Models名称空间中):

@model IEnumerable
现在,您可以使用intellisense liek迭代整个过程:

@foreach (var item in Model)
{
    <p>item.Topic</p>
    <p>item.Signature</p>
    <p>item.Time</p>
}
@foreach(模型中的变量项)
{
项目.主题

项目.签字

项目.时间

}
你说的“正常代码”是什么意思?什么是
ViewBag.opis
?你指的是哪些列表?@StephenMuecke当然我忘了添加视图包。很抱歉VieBag.opis是包含汇总项目的列表。所谓“普通代码”,我指的是C#中的代码,在我的foreach循环中,而不是在视图中。我不是母语人士,所以可能会被称为不同。对我来说,你应该选择model而不是ViewBag。您可以在整个viewbag中进行迭代,但需要将其转换为某种类型—它是一种动态类型—在运行时解析的类型。所以你可以迭代,但你没有这方面的知识。没有测试代码,因为我不在,但我喜欢这个想法!这将引发异常。。。调用Disposed XmlReader上的Close,这在using语句之外是不可访问的。而且您不再需要此ViewBagsRemember-仅当您想传递一些额外的数据时才使用ViewBag。模型方法更好!(这就是为什么它被称为模型视图控制器而不是ViewBag视图控制器)