I'm trying to make a simple chat app to get started with ASP.NET and my partial view seems to be recieving the data type passed to Index.cshtml instead of the data I intended to pass to PersistantMessagesPartial.cshtml.
Error:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[WebChat.Areas.Identity.Data.AppUser]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List1[WebChat.Models.MessageModel]'.
In HomeController.cs:
public async Task<IActionResult> Index()
{
Debug.WriteLine("debug1");
return View(await _AppUserDBContext.userList.ToListAsync());
}
public async Task<IActionResult> _PersistantMessagesPartial()
{
Debug.WriteLine("debug2");
return PartialView("_PersistantMessagesPartial", await _MessageModelDBContext.messageList.ToListAsync());
}
In _PersistantMessagesPartial.cshtml:
@model List<WebChat.Models.MessageModel>
<script>
console.log("test");
</script>
@foreach(var item in Model)
{
<script>
console.log("MessagePartial");
console.log(@item.Contents);
</script>
}
How i render the partial, in _Layout.cshtml:
<partial name="_PersistantMessagesPartial" />
Index.cshtml recieves a list of AppUser, and that works correctly. I'm not sure how to make _PersistantMessagesPartial recieve a list of MessageModel instead of the list of AppUser.
modelto your partial view tag. Like this:<partial name="_PersistantMessagesPartial" model="@Model.GoesHere" />. I think it is getting the model from your Index view passed to it. It doesn't call your_PersistantMessagesPartialin your controller, you might need to make a view model or turn the partial view and action into a View Component - zgoodViewDataDictionary.. - SRQ Coder