繁体   English   中英

如何使用foreach在ASP.NET中呈现数据?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM