简体   繁体   English

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

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

I've been learning how to use the Xero API with my ASP Core MVC Project.我一直在学习如何在我的 ASP Core MVC 项目中使用 Xero API。

I have made a model for the Contact.cs which is declared as so:我为 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; }
    }

I then have my ContactViewModel.cs for a singular Contact which is as follows:然后我将我的 ContactViewModel.cs 用于单个联系人,如下所示:

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

Finally, I have my ContactsViewModel.cs for the list of Contacts:最后,我有我的 ContactsViewModel.cs 用于联系人列表:

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


    }

I am trying to make an API call from the Contacts endpoint, but I do not want all the values displayed.我正在尝试从 Contacts 端点进行 API 调用,但我不想显示所有值。 My targetted output would look like so:我的目标输出如下所示:

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

I have generated a Contacts razor view page that has the following code:我生成了一个联系人剃刀视图页面,其中包含以下代码:

@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>

I am trying to display that output when users go to the localhost:5000/home/contacts page.当用户转到 localhost:5000/home/contacts 页面时,我试图显示该输出。

Within my HomeController.cs I have the following code:在我的 HomeController.cs 中,我有以下代码:

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

    return View(model);
}

But for some reason, I am being greeted with the following error但由于某种原因,我收到以下错误在此处输入图片说明

What is the cause for this?这是什么原因? And how could I pass the List of users in a better way if the way I've done it is wrong?如果我的做法是错误的,我怎么能以更好的方式传递用户列表?

Thank you all in advance谢谢大家

It looks like you're passing a single instance of the object instead of the array you declared.看起来您正在传递对象的单个实例而不是您声明的数组。 Try this following:试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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