![](/img/trans.png)
[英]How can I add a dynamic button in ASP.NET with db data in a foreach?
[英]How can I render my data in ASP.NET with foreach?
我是ASP.NET的新手,我从Udemy的视频中学习ASP.NET,这是我的代码
Customer.cs(模型)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Vidly.Models
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
}
CustomersController.cs(控制器)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;
namespace Vidly.Controllers
{
public class CustomersController : Controller
{
//
// GET: /Customers/
public ActionResult Index()
{
var customers = new List<Customer>
{
new Customer {Name = "Mahdi" },
new Customer {Name = "Yaser" },
new Customer {Name = "Zahra" },
new Customer {Name = "Amir" },
};
return View(customers);
}
}
}
Index.cshtml(查看)
@model Vidly.Models.Customer
<h3>Customer page</h3>
<table class="table table-striped table-hover table-‐bordered">
<thead>
<tr>
<th>Customer</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td><a href="#"> @customer.Name </a></td>
</tr>
}
</tbody>
</table>
visual studio告诉我问题是我的@foreach
但我不知道如何解决! 感谢您的帮助;)
更改
@model Vidly.Models.Customer
至
@model IEnumerable<Vidly.Models.Customer>
在index.cshtml
,检查您的模型。 您必须将其替换为:
@model List<Vidly.Models.Customer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.