为什么在将 ViewModel 传递给 View 时出现此错误?

Why do I get this error when passing ViewModel to View?

将 ViewModel 传递给 View 时出现错误

The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List'1[TraficAlert.Models.TaBarHeader]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable'1[TraficAlert.Models.ViewModels.HeaderTelegramaViewModel]'.

我尝试在 Index.cshtml 中使用 @model IEnumerable<TraficAlert.Models.ViewModels.HeaderTelegramaViewModel> 并且它有效,但我需要从 HeaderTelegramaViewModel.

访问 属性

Index.cshtml:

@model IEnumerable<TraficAlert.Models.ViewModels.HeaderTelegramaViewModel>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.TaBarHeader.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TaBarHeader.ParentId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TaBarHeader.TStamp)
            </th>
   (...)
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.TaBarHeader.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.TaBarHeader.ParentId)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.TaBarHeader.TStamp)
                </td>
   (...)
                <td>
                    <a asp-action="Edit" asp-route-id="@item.TaBarHeader.Id">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.TaBarHeader.Id">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.TaBarHeader.Id">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

HeaderTelegramaController:

(...)
    public IActionResult Index()
    {
        var applicationDbContext = _unitofwork.BarHeader.GetAllBarH().ToList();
        return View(applicationDbContext);
    }

TaBarHeaderRepository:

public IEnumerable<TaBarHeader> GetAllBarH()
{
    return _db.TaBarHeaders
        .Include(t => t.CategoriaFk)
        .Include(t => t.CauzaFk)
        .Include(t => t.ClasaFk)
        .Include(t => t.LucrareFk)
        .Include(t => t.RegionalaFk)
        .Include(t => t.UserFk);
}

HeaderTelegramaViewModel:

public TaBarHeader TaBarHeader { get; set; }
public IEnumerable<SelectListItem> Categoria { get; set; }
public IEnumerable<ViewOtf> ViewOTFCollection { get; set; }
(...)

为什么会出现上述错误?

谢谢。

使用:: @model TraficAlert.Models.ViewModels.HeaderTelegramaViewModel

而不是 ::@model IEnumerable

在 index.cshtml 页的顶部

查看模型的类型 IEnumerable 并应用此:

   public IActionResult Index()
   {
       var applicationDbContext = _unitofwork.BarHeader.GetAllBarH();
       return View(applicationDbContext);
   }

我认为可能的错误可能来自您的 table 头脑 考虑到您的模型是 IEnumerable.

,请尝试指定一个索引

所以改变

@Html.DisplayFor(modelItem => item.TaBarHeader.Id)

像这样

@Html.DisplayFor(modelItem => item[0].TaBarHeader.Id)

在 cshtml 中使用下面的模型。

@model TraficAlert.Models.ViewModels.HeaderTelegramaViewModel

并在 Index() 中创建一个 HeaderTelegramaViewModel 实例:

var _HeaderTelegramaViewModel = new HeaderTelegramaViewModel();
_HeaderTelegramaViewModel.TaBarHeader = TaBarHeader;

并且 class HeaderTelegramaViewModel 必须具有:

public IEnumerable<TaBarHeader> TaBarHeader { get; set; } 
public IEnumerable<SelectListItem> Categoria { get; set; } 
public IEnumerable<ViewOtf> ViewOTFCollection { get; set; }

错误消息非常清楚地解释了问题:您传递的类型与视图预期的类型不同。

具体来说,您调用 GetAllBarH() 来获取视图的数据,它 returns IEnumerable<TaBarHeader>。因此页面的 model 声明应该是:

@model IEnumerable<TraficAlert.Models.TaBarHeader>

如果您真的想要 HeaderTelegramaViewModel,那么您将不得不以某种方式转换 IEnumerable<TaBarHeader>。我假设您错过了控制器中的那一步。

Why do I get the above mentioned error?

因为你的actionreturned的数据类型数据不一样视图上需要类型。

您可以像这样修改您的 HeaderTelegramaController

public IActionResult Index()
{
   var applicationDbContext = _unitofwork.BarHeader.GetAllBarH().Select(m => new HeaderTelegramaViewModel { TaBarHeader = m }).ToList();
   return View(applicationDbContext);
}