亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 Entity Framework Core 將列表傳遞給視圖

通過 Entity Framework Core 將列表傳遞給視圖

C#
慕桂英546537 2022-07-23 09:39:14
我想將產品列表傳遞給我的視圖。但我不知道我在這里錯過了什么。當我運行它時,我遇到了這個異常InvalidOperationException:傳遞到 ViewDataDictionary 的模型項的類型為“System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.Collections.Generic.IList1[DataLayers.Models.mymodel],DataLayers.Services.Repository+d__2]”,但是這個ViewDataDictionary 實例需要“System.Collections.Generic.IEnumerable`1[DataLayers.Models.mymodel]”類型的模型項。這是我的IRepositoryTask<IList<mymodel>> GetAllProductsAsync();這是我的存儲庫(為簡單起見,省略了上下文注入)public async Task<IList<mymodel>> GetAllProductsAsync()    {        return await _context.mymodel.ToListAsync();    }這是我的控制器private readonly IRepository _IRepository;public MyController(IRepository ThisController){      _IRepository = ThisController;}public IActionResult Products(){  return View(_IRepository.GetAllProductsAsync());}最后這是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 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

考慮改變:


public IActionResult Products()

{

  return View(_IRepository.GetAllProductsAsync());

}

至:


public async Task<IActionResult> Products()

{

  return View(await _IRepository.GetAllProductsAsync());

}


查看完整回答
反對 回復 2022-07-23
  • 1 回答
  • 0 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號