在ASP.NET MVC中,可以使用HTML辅助方法来生成包含模型数据的表格行。以下是一个示例代码:
假设模型类如下所示:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
在控制器中,将模型数据传递给视图:
public ActionResult Index()
{
List people = new List
{
new Person { Id = 1, Name = "John Doe", Email = "john.doe@example.com" },
new Person { Id = 2, Name = "Jane Smith", Email = "jane.smith@example.com" }
};
return View(people);
}
在视图中,使用HTML辅助方法生成表格,并在表格行中包含模型数据的文本链接:
@model List
ID
Name
Email
@foreach (var person in Model)
{
@person.Id
@Html.ActionLink(person.Name, "Details", new { id = person.Id })
@person.Email
}
上述代码中,使用@foreach循环遍历模型数据,并在每个表格行中使用Html.ActionLink方法生成包含模型数据的文本链接。ActionLink方法的第一个参数是文本,第二个参数是操作方法名称,第三个参数是路由值(在此示例中,将id作为路由值传递给Details操作方法)。
通过这种方式,可以将模型数据动态地显示为表格行,并在其中包含文本链接。