本文介绍了通过收集剃刀和印刷指数及项目迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的Razor视图的问题。我有以下几点:
I'm having problems with my razor view. I have the following:
public ICollection<Topic> Topics
public class Topic
{
public string Description { get; set; }
}
我想通过收集迭代并打印出的结果是这样的:
I want to iterate through the collection and print out the results like this:
@foreach (int index in Enumerable.Range(0, Model.Topics.Count())){
<div>@(index). Model.Topics[@(index)].Description"</div>
}
问题是,我得到的是:
The problem is that all I get is:
0. Model.Topics[0].Description"
1. Model.Topics[1].Description"
我试过各种各样的东西,但仍不能得到描述出来。
I tried all kinds of things but still can't get the description out.
我是什么做错了: - (
What am I doing wrong :-(
推荐答案
尝试这样的:
@foreach (var item in Model.Topics.Select((model, index) => new { index, model }))
{
<div>@(item.index). @item.model.Description</div>
}
这篇关于通过收集剃刀和印刷指数及项目迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!