如何根据字符串列表渲染段落?

huangapple go评论66阅读模式
英文:

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&lt;string&gt; names = new List&lt;string&gt;
names.add(&quot;Foo&quot;);
names.add(&quot;Bar&quot;);

How would I make that generate something along the lines of

&lt;div&gt;
&lt;p&gt;Foo&lt;/p&gt;
&lt;p&gt;Bar&lt;/p&gt;
&lt;/div&gt;

答案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&lt;string&gt; model = new List&lt;string&gt;();
        model .Add(&quot;Foo&quot;);
        model .Add(&quot;Bar&quot;);

        return View(model );
    }

Then just add this into your view:

@model IList&lt;String&gt;


&lt;div&gt;
@for (int i = 1; i &lt; Model.Count; i++)
{
    &lt;p&gt;@Model[i]&lt;/p&gt;
}&lt;/div&gt;

huangapple
  • 本文由 发表于 2020年1月3日 19:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定