InvalidOperationException:由于提交的列表表单不正确而显示错误

InvalidOperationException: Error Being displayed due to incorrect List form being submitted

我一直在学习如何在我的 ASP 核心 MVC 项目中使用 Xero API。

我为 Contact.cs 制作了一个模型,声明如下:

 public class Contact
    {
        public string ContactID { get; set; }
        public string ContactStatus { get; set; }
        public string Name { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
        public string SkypeUserName { get; set; }
        public string BankAccountDetails { get; set; }
        public string TaxNumber { get; set; }
        public string AccountsReceivableTaxType { get; set; }
        public string AccountsPayableTaxType { get; set; }
        public List<Address> Addresses { get; set; }
        public List<Phone> Phones { get; set; }
        public DateTime UpdatedDateUTC { get; set; }
        public bool IsSupplier { get; set; }
        public bool IsCustomer { get; set; }
        public string DefaultCurrency { get; set; }
    }

然后我有一个单独的联系人 ContactViewModel.cs,如下所示:

public class ContactViewModel
    {
        public bool isCustomer { get; set; }
        public bool isSupplier { get; set; }
    }

最后,我有我的 ContactsViewModel.cs 联系人列表:

public class ContactsViewModel
    {
        public ContactViewModel[] Contacts { get; set; }


    }

我正在尝试从联系人端点进行 API 调用,但我不想显示所有值。我的目标输出如下所示:

Contact 1 - Retailer
Contact 2 - Customer
Contact 3 - Retailer
... Etc

我生成了包含以下代码的联系人剃刀视图页面:

@model IEnumerable<XeroOAuth2Sample.Models.Contact>

@{
    ViewData["Title"] = "Contacts";
}

<h1>Contacts</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ContactID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ContactStatus)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.EmailAddress)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.SkypeUserName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.BankAccountDetails)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TaxNumber)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.AccountsReceivableTaxType)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.AccountsPayableTaxType)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.UpdatedDateUTC)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsSupplier)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsCustomer)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DefaultCurrency)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ContactID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContactStatus)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmailAddress)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SkypeUserName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.BankAccountDetails)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TaxNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AccountsReceivableTaxType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AccountsPayableTaxType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UpdatedDateUTC)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsSupplier)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsCustomer)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DefaultCurrency)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
}
    </tbody>
</table>

我试图在用户转到 localhost:5000/home/contacts 页面时显示该输出。

在我的 HomeController.cs 中,我有以下代码:

[HttpGet]
[Authorize]
public IActionResult Contacts()
{
    var model = new Contact();

    return View(model);
}

但由于某种原因,我遇到了以下错误

这是什么原因?如果我做的方式是错误的,我怎么能以更好的方式传递用户列表?

提前谢谢大家

您似乎传递的是对象的单个实例,而不是您声明的数组。尝试以下操作:

[HttpGet]
[Authorize]
public IActionResult Contacts()
{
    var model = new Contact();
    var contacts = new Contact[] { model };
    return View(contacts);
}