我有一个返回字符串列表的方法。我只是想在视图中以纯文本形式显示该列表。
这是控制器的列表:
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();
}
}
和视图:
<table class="adminContent">
<tr>
<td>HEJ</td>
</tr>
<tr>
<td>@Html.Action("Service", "Service")</td>
</tr>
</tr>
</table>
我认为我必须在视图中执行诸如foreach循环之类的操作,并使用“ @”引用列表,但是如何?
您的操作方法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());
}
在此引用之后,您的视图中的模型将:
@model List<string>
@foreach (var element in Model)
{
<p>@Html.DisplayFor(m => element)</p>
}
在我的示例中,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);
}
结果是:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句