2

I want to pass a list of products to my view. but I Don't know what I'm missing here. When I run it i face with this exception

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.Collections.Generic.IList1[DataLayers.Models.mymodel],DataLayers.Services.Repository+d__2]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable`1[DataLayers.Models.mymodel]'.

This is my IRepository

Task<IList<mymodel>> GetAllProductsAsync();

And here is my Repository (Context injection is omitted for the sake of simplicity )

public async Task<IList<mymodel>> GetAllProductsAsync()
    {
        return await _context.mymodel.ToListAsync();
    }

This is MyController

private readonly IRepository _IRepository;
public MyController(IRepository ThisController)
{
      _IRepository = ThisController;
}


public IActionResult Products()
{
  return View(_IRepository.GetAllProductsAsync());
}

And finally Here is myView

@model IEnumerable<DataLayers.Models.mymodel>
<table>
    @foreach (var item in Model)
        {
            <tr class="table-row">
                <td>
                    <img src="@Html.DisplayFor(m => item.DishImg)">
                </td>
                <td>
                    <p>@Html.DisplayFor(m => item.Productname)</p>
                </td>
                <td>
                    <p>@Html.DisplayFor(m => item.ProductInfo)</p>
                </td>
                <td>
                    <p>@Html.DisplayFor(m => item.ProductPrice)</p>
                </td>
            </tr>
        }
</table>
1
  • 1
    Have you tried adding await like this.. return View(await _IRepository.... Commented Jan 15, 2019 at 1:13

1 Answer 1

3

Consider change from:

public IActionResult Products()
{
  return View(_IRepository.GetAllProductsAsync());
}

To:

public async Task<IActionResult> Products()
{
  return View(await _IRepository.GetAllProductsAsync());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.