InvalidOperationException: ошибка, отображаемая из-за неправильной отправки формы списка - PullRequest
0 голосов
/ 28 февраля 2020

Я изучаю, как использовать Xero API с моим ASP Core MVC Проектом.

Я создал модель для 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>

Я пытаюсь отобразить этот вывод, когда пользователи go для localhost: 5000 / home / contacts page.

В моем HomeController.cs у меня есть следующий код:

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

    return View(model);
}

Но по какой-то причине меня приветствуют с следующая ошибка enter image description here

В чем причина? И как я могу передать Список пользователей лучше, если я поступлю неправильно?

Заранее всем спасибо

1 Ответ

1 голос
/ 28 февраля 2020

Похоже, вы передаете один экземпляр объекта вместо объявленного вами массива. Попробуйте следующее:

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