MVC 代码,在 index.cstml 上抛出错误,非静态字段错误

MVC code, throwing error on index.cstml, non-static field error

我有这个 index.cshtml 代码:

@model IEnumerable<Desa.Models.Person>

<p>Person List Page</p>

<table border="1">
    <tr>
        <th>Name</th>
        <th>Address</th>
        <th>Phone</th>
        <th>Email</th>
    </tr>
     @foreach (var item in Model)
        {
        <tr>
            <td>@Person.name</td>
            <td>@Person.email</td>
            <td>@Person.phone  </td>
            <td>@Person.address</td>
        </tr>
        }
    </table>

Person 模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Desa.Models
{
    public class Person
    {
        [Key]
        public string name { get; set; }
        public string email { get; set; }
        public string phone { get; set; }
        public string address { get; set; }
    }
}

Index.cshtml 部分,它抛出一条错误消息:

CS0120 An object reference is required for the non-static field, method, or property 'Person.name' Desa
C:\Users\qendr\source\repos\Desa\Desa\Views\Person\Index.cshtml 15
Active

我不太确定可能是什么问题,有什么帮助吗?

您正在使用 foreach 迭代 IEnumerable<Desa.Models.Person> collection。因此有必要参考下面的 collection 项:

@foreach (var item in Model)
{
<tr>
    <td>@item.name</td>
    <td>@item.email</td>
    <td>@item.phone  </td>
    <td>@item.address</td>
</tr>
}

有关详细信息,请参阅 Microsoft 文档:Strongly typed views