英文:
How do I render paragraphs based on a list of strings?
问题
所以我好奇如何基于我在List<string>中的内容来呈现div内的段落...
所以想象一下。
List<string> names = new List<string>
names.add("Foo");
names.add("Bar");
我该如何生成类似以下的内容:
<div>
<p>Foo</p>
<p>Bar</p>
</div>
英文:
So I'm curious to how I would render paragraphs inside a div based on the content I have in a List<string>...
So picture this.
List<string> names = new List<string>
names.add("Foo");
names.add("Bar");
How would I make that generate something along the lines of
<div>
<p>Foo</p>
<p>Bar</p>
</div>
答案1
得分: 2
将名称列表添加到您的模型中,或者将其作为控制器中的模型(在下面的代码中,我将其作为模型传递)。
public ActionResult Home()
{
List<string> model = new List<string>();
model.Add("Foo");
model.Add("Bar");
return View(model);
}
然后只需将以下内容添加到您的视图:
@model IList<String>;
<div>
@for (int i = 1; i < Model.Count; i++)
{
<p>@Model[i]</p>
}</div>
英文:
Add the names List into your Model Or as you model in the controller (in the code below i pass it as the model).
public ActionResult Home()
{
List<string> model = new List<string>();
model .Add("Foo");
model .Add("Bar");
return View(model );
}
Then just add this into your view:
@model IList<String>
<div>
@for (int i = 1; i < Model.Count; i++)
{
<p>@Model[i]</p>
}</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论