C# 在视图中显示列表项
我对c和MVC还很陌生 我使用如下方法创建了一个FormController.cs:C# 在视图中显示列表项,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我对c和MVC还很陌生 我使用如下方法创建了一个FormController.cs: public ActionResult Showlist() { List<int> list = new List<int>(); list.Add(54); list.Add(524); list.Add(23); list.Add(43); return View(list);
public ActionResult Showlist()
{
List<int> list = new List<int>();
list.Add(54);
list.Add(524);
list.Add(23);
list.Add(43);
return View(list);
}
public ActionResult Showlist()
{
列表=新列表();
增加(54);
增加(524);
增加(23);
增加(43);
返回视图(列表);
}
然后,我从该方法创建一个视图。在我的div标签中,我放置了如下文本:
<body>
<div>
@Model.ToString();
</div>
</body>
@Model.ToString();
结果是一个带有文本System.Collections.Generic.List`1[System.Int32]
我的问题:显示我在列表中添加的值的好(或最好)方法是什么?此外,您还需要在cshtml中将模型声明为列表:
@model IEnumerable<int>
@{
foreach (var anInt in Model)
{
<text>@anInt</text>
}
}
@model IEnumerable
@{
foreach(模型中的变量)
{
@阿尼特
}
}
如果他没有声明模型,他将得到一个NullReferenceException,而不是System.Collections.Generic.List
1[System.Int32]`@Selman…明白了。我只是为了清楚起见才加上它。不是每个人都知道这一点。