Error: The model item passed into the ViewDataDictionary is of type '' In Asp.Net Core
In my project web Asp.net core 3.1. I have a view component to generate head navigation for my website but when I invoke it into _Layout.cshtml
page I got an error: The model item passed into the ViewDataDictionary is of type '', but this ViewDataDictionary instance requires a model item of type ''
System.InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Models.HeaderModels', but this ViewDataDictionary instance requires a model item of type 'Models.BannerMenuModel'.
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(Object value)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary..ctor(ViewDataDictionary source, Object model, Type declaredModelType)
at lambda_method(Closure , ViewDataDictionary )
at Microsoft.AspNetCore.Mvc.Razor.RazorPagePropertyActivator.CreateViewDataDictionary(ViewContext context)
at Microsoft.AspNetCore.Mvc.Razor.RazorPagePropertyActivator.Activate(Object page, ViewContext context)
at Microsoft.AspNetCore.Mvc.Razor.RazorPageActivator.Activate(IRazorPage page, ViewContext context)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)
This is the view of ViewComponent:
@model BannerMenuModel
@{
string requestUrl = Context.Request.Path.ToString().ToLower();
var typeSuggestionSearch = 0;
}
<div class="header box_sd">
<div class="container">
<div class="header-center">
<div class="row">
<div class="col-xs-2">
<div class="logo f-logo-header"><a href="/"><img class="lazyload-img" src="/Web/Images/logo.png" alt=""></a></div>
</div>
<div class="col-xs-5">
<ul class="navbar_menu">
<li>
<a href="/car-sale" title="Used car">Used car</a>
</li>
<li class="level1 hassub">
<a href="javascript:;" rel="nofollow" title="Used car">Used car <i class="icon-down-open"></i></a>
<div class="dropdown-menu style3">
<div class="home-logocar">
<div class="col">
<div class="brand-item">
<div class="photo">
<a href="/car-toyota" title="Toyota"><img class="lazyload-menu menu-size" data-src="/Web/Images/logo-car/toyota.png" alt="Toyota"></a>
</div>
<div class="info text-center">
<h3 class="title"><a href="/car-toyota" title="Toyota">Toyota</a></h3>
</div>
@if (Model != null && Model.ListToyota != null && Model.ListToyota.Any())
{
<ul class="dropdown-carlist">
@foreach (var item in Model.ListToyota)
{
<li><a href="@item.KeywordForSaleDto.Link">@item.KeywordForSaleDto.Title.Replace("Car ", "")</a></li>
}
</ul>
}
</div>
</div>
<div class="col">
<div class="brand-item">
<div class="photo">
<a href="/car-honda" title="Honda"><img class="lazyload-menu menu-size" data-src="/Web/Images/logo-car/honda.png" alt="Honda"></a>
</div>
<div class="info text-center">
<h3 class="title"><a href="/car-honda" title="Honda">Honda</a></h3>
</div>
@if (Model != null && Model.ListHonda != null && Model.ListHonda.Any())
{
<ul class="dropdown-carlist">
@foreach (var item in Model.ListHonda)
{
<li><a href="@item.KeywordForSaleDto.Link">@item.KeywordForSaleDto.Title.Replace("Car ", "")</a></li>
}
</ul>
}
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
And this is the code behind of ViewComponent:
public async Task<IViewComponentResult> InvokeAsync()
{
try
{
HeaderModels model = new HeaderModels();
model.ListToyota = await _keywordService.GetKeywordOnPageForSaleByBrand("toyota");
model.ListHonda = await _keywordService.GetKeywordOnPageForSaleByBrand("honda");
model.ListMercedesBenz = await _keywordService.GetKeywordOnPageForSaleByBrand("mercedes-benz");
model.ListMitsubishi = await _keywordService.GetKeywordOnPageForSaleByBrand("mitsubishi");
model.ListNissan = await _keywordService.GetKeywordOnPageForSaleByBrand("nissan");
model.ListBMW = await _keywordService.GetKeywordOnPageForSaleByBrand("bmw");
model.ListIsuzu = await _keywordService.GetKeywordOnPageForSaleByBrand("isuzu");
model.ListMazda = await _keywordService.GetKeywordOnPageForSaleByBrand("mazda");
model.ListSuzuki = await _keywordService.GetKeywordOnPageForSaleByBrand("suzuki");
model.ListFord = await _keywordService.GetKeywordOnPageForSaleByBrand("ford");
model.ListBoxLinkMenu = (await _boxLinkService.GetBoxLinkMenuByArticleType((int)ArticlesType.Rate, Const.BoxLinkMenu)).ToList();
return View(model);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return View(new HeaderModels());
}
}
Thanks for any suggestions.
-
S1
Simeon Ezechinyere Mar 01 2021
This error occurs when the model return in InvokeAsyn() method not the same as the model declare in your view.
Model in view is
BannerMenuModel
and model return in your InvokeAsyn() isHeaderModels
. Please make sure they are the same model.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.