@GenTs666

Ошибка при создании RenderPartialAsync, проблема в контроллере?

Еесть контроллер Index, в нем сопоставляю данные из бд с modelview и мой View забирает данные пользователя и отображает их. И соответственно ниже прикреплю PartialView
public class CustomerController : Controller
    {
        private ICustomerRepository _customerRepository;
        public CustomerController(ICustomerRepository customerRepository)
        {
            _customerRepository = customerRepository;
        }
        [HttpGet]
        public IActionResult Index()
        {
          IEnumerable<CustomerViewModel> customers =
        _customerRepository.GetAllCustomers().Select(s => new
        CustomerViewModel
        {
            CustomerId = s.CustomerId,
            Name = s.Name,
            Adress = s.Adress
        });
            return View("Index", customers);
        }
        [HttpGet]
        public IActionResult Create()
        {
            return Redirect("Index");
        }

    }

@model IEnumerable<CustomerViewModel>
<h2>Create Customer</h2>
@{
    await Html.RenderPartialAsync("Create");
}
    <table class="table">
    @Html.DisplayNameFor(model => model.Name)
    @foreach (var item in Model)
    {
                @Html.DisplayFor(modelItem => item.Name)
    }
</table>


Это сам PartialView
@model CustomerViewModel
        <div class="col-md-4">
            <form asp-action="Create" asp-controller="Customer">

                <div class="form-group">
                    <label asp-for="Name" class="control-label"></label>
                    <input type="text" asp-for="Name" class="form-control" />
                </div>


При запуске приложения вылетает ошибка:
InvalidOperationException: The model item passed into the
ViewDataDictionary is of type 'System.Linq.Enumerable+SelectEnumerableIterator`
2[Store.DAL.Entity.Customer,Store.Web.ViewModels.CustomerViewModel]', but this
ViewDataDictionary instance requires a model item of type 'Store.Web.ViewModels.
CustomerViewModel


Store.Web.Pages.Customer.Views_Customer_Index.ExecuteAsync() in Index.cshtml
-
@{
    ViewData["Title"] = "Customer";
}
<h2>Create Customer</h2>
@{
    await Html.RenderPartialAsync("Create");
}
<h2>Customers</h2>
<table class="table">
    <tr>


Если partialView вынести на отдельную страницу всмысле просто создать ссылку на View, то всё отобразится и ошибки не будет. Может всё дело в тот как я переопределяю данные в Контроллере для customerViewModel?
Как вы думаете в чём причина ?
  • Вопрос задан
  • 98 просмотров
Решения вопроса 1
@Ascar
У вас передается родительская модель. Передайте так:
@await Html.PartialAsync("Create", new CustomerViewModel())

Либо в представлении поменяйте модель.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы