Search code examples
c#asp.net-mvcasp.net-coreviewviewmodel

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


When passing ViewModel to View I get the error

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]'.

I have tried to use @model IEnumerable<TraficAlert.Models.ViewModels.HeaderTelegramaViewModel> in the Index.cshtml and it works, but I need to access a property from 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; }
(...)

Why do I get the above mentioned error?

Thank you.


Solution

  • use the model below in the cshtml.

    @model TraficAlert.Models.ViewModels.HeaderTelegramaViewModel
    

    And in the Index() create an instance of HeaderTelegramaViewModel:

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

    And the class HeaderTelegramaViewModel must have:

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