G
G
GenTs6662019-04-14 10:51:33
ASP.NET
GenTs666, 2019-04-14 10:51:33

Error creating RenderPartialAsync, problem in controller?

There is an Index controller, in it I compare the data from the database with the modelview and my View takes the user's data and displays them. And accordingly below I will attach a 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>

This is the PartialView itself
@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>

When starting the application, an error occurs:
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>


If the partialView is taken out on a separate page, in the sense of simply creating a link to the View, then everything will be displayed and there will be no error. Maybe it's all about how I override the data in the Controller for the customerViewModel?
What do you think is the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ascar, 2019-04-14
@GenTs666

You are passing the parent model. Pass like this:
Or change the model in the view.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question