在Asp.Net MVC中,可以通过以下步骤将列表返回给视图:
using System.Collections.Generic;
using System.Web.Mvc;
public class HomeController : Controller
{
public ActionResult Index()
{
// 创建一个示例列表
List myList = new List();
myList.Add("Item 1");
myList.Add("Item 2");
myList.Add("Item 3");
// 将列表传递给视图
return View(myList);
}
}
@model
指令将模型类型设置为列表的类型。@model List
List View
List View
@foreach (var item in Model)
{
- @item
}
在上述代码中,我们使用@model
指令将模型类型设置为List
,并使用@foreach
循环遍历列表中的每个项目,并将其显示为一个列表项。
通过以上步骤,你可以成功将列表返回给视图,并在视图中显示列表的内容。