My classes:
public abstract class BaseClass
{
public string Id { get; set; }
}
public abstract class BaseItemTraduisible : BaseClass
{
public string Code { get; set; }
}
public abstract class BaseTrad<TypeReference> : BaseClass
{
public string Libelle { get; set; }
public string IdReference { get; set; }
public TypeReference ObjReference { get; set; }
}
public class BaseTradVM<Item,Trad>
where Item : BaseItemTraduisible
where Trad : BaseTrad<Item>
{
public List<Item> Items { get; set; }
public List<Trad> Trads { get; set; }
public List<Langue> Langues { get; set; }
public Item ItemVide { get; set; }
public Trad TradVide { get; set; }
public string Titre { get; set; }
}
public class CorpsDeMetierVM : BaseTradVM<CorpsDeMetier, CorpsDeMetierTrad>
{
}
public partial class CorpsDeMetier : BaseItemTraduisible
{
public IList<CorpsDeMetierTrad> CorpsDeMetierTrads { get; set; }
}
public partial class CorpsDeMetierTrad : BaseTrad<CorpsDeMetier>
{
}
My controller :
// GET: CorpsDeMetiers
public async Task<IActionResult> Index()
{
var cdmvm = new CorpsDeMetierVM();
cdmvm.Items = _context.CorpsDeMetiers.Include(c => c.CorpsDeMetierTrads).ToList();
cdmvm.Trads = _context.CorpsDeMetierTrads.Include(c => c.Langue).ToList();
cdmvm.Langues = _context.Langues.OrderBy(l => l.Ordre).ToList();
cdmvm.ItemVide = new CorpsDeMetier();
cdmvm.TradVide = new CorpsDeMetierTrad();
cdmvm.Titre = "LibelleCorpsDeMetier";
return View("Views/ConfigurationTrad/index.cshtml", cdmvm);
}
My view get the model like that :
@model ArchiEnLigne.Models.BaseTradVM<BaseItemTraduisible,BaseTrad<BaseItemTraduisible>>
The error message :
InvalidOperationException
: The model item passed into theViewDataDictionary
is of type 'ArchiEnLigne.Models.CorpsDeMetierVM
', but thisViewDataDictionary
instance requires a model item of type 'ArchiEnLigne.Models.BaseTradVM``2[ArchiEnLigne.Data.Entities.BaseItemTraduisible,ArchiEnLigne.Data.Entities.BaseTrad``1[ArchiEnLigne.Data.Entities.BaseItemTraduisible]]
'.
StackTrace:
´´´
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary..ctor(ViewDataDictionary source, object model, Type declaredModelType) lambda_method(Closure , ViewDataDictionary ) Microsoft.AspNetCore.Mvc.Razor.RazorPagePropertyActivator.CreateViewDataDictionary(ViewContext context) Microsoft.AspNetCore.Mvc.Razor.RazorPagePropertyActivator.Activate(object page, ViewContext context) Microsoft.AspNetCore.Mvc.Razor.RazorPageActivator.Activate(IRazorPage page, ViewContext context) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts) Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable statusCode) Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result) Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0<TFilter, TFilterAsync>(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
´´´
I don't know why it can't make the conversion when the hierarchy is correct to me
I hope someone can help me
Thx for read me
StackTrace
of the exception. The error isn't in the code you posted but with your ASP.NET MVC view file's@model
declaration. – Dai