Who*_*AmI 5 .net c# asp.net-mvc ienumerable list
我有一个返回字符串列表的方法.我只是想在视图中以纯文本形式显示该列表.
这是控制器的列表:
public class ServiceController : Controller
{
public string Service()
{
//Some code..........
List<string> Dates = new List<string>();
foreach (var row in d.Rows)
{
Dates.Add(row[0]);
}
return Dates.ToString();
}
public ActionResult Service()
{
Service();
}
}
Run Code Online (Sandbox Code Playgroud)
并且观点:
<table class="adminContent">
<tr>
<td>HEJ</td>
</tr>
<tr>
<td>@Html.Action("Service", "Service")</td>
</tr>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我认为我必须在视图中做一些像foreach循环一样的东西并使用"@"引用列表但是如何?
Mar*_*rco 14
你的行动方法Service应该返回一个View.在此之后将Service()方法的返回类型更改string为List<string>
public List<string> Service()
{
//Some code..........
List<string> Dates = new List<string>();
foreach (var row in d.Rows)
{
Dates.Add(row[0]);
}
return Dates;
}
public ActionResult GAStatistics()
{
return View(Service());
}
Run Code Online (Sandbox Code Playgroud)
在此之后参考视图中的模型:
@model List<string>
@foreach (var element in Model)
{
<p>@Html.DisplayFor(m => element)</p>
}
Run Code Online (Sandbox Code Playgroud)
在我的例子中,ActionResult看起来像这样:
public ActionResult List()
{
List<string> Dates = new List<string>();
for (int i = 0; i < 20; i++)
{
Dates.Add(String.Format("String{0}", i));
}
return View(Dates);
}
Run Code Online (Sandbox Code Playgroud)
这导致了输出:

您可以在视图中执行以下操作,
@foreach (var item in @Model)
{
<li>@item.PropertName</li>
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76155 次 |
| 最近记录: |