Angela Angela - 10 months ago 66
ASP.NET (C#) Question

Partial View Rending Error the model item passed into the dictionary is of type

So I have developed just a simple partial view that will handle displaying messages at the top of the page.

I then want to use that partial view in all my pages to handle in informational, warning, or error messages.

I created a logon page and it is my first attempt at doing this.

It is giving me an error stating I am not passing in the correct type. I don't understand why it is stating that the dictionary type I am trying to pass in is of Music.Accounts.ViewModels.Log when it is very clearly shown I develop a variable assign the property from the parent model to that variable and then try and pass that in as my model to my partial view.

Any help would be greatly appreciated :)

Error and Code is below

Thanks :)

System Error when rendering page

Additional information: The model item passed into the dictionary is of type
'Music.Accounts.ViewModels.LogOn', but this dictionary requires a model item
of type 'System.Collections.Generic.List`1


Class

namespace MMLibrary.MMWeb.MessageHandling
{
public enum MessageTypes
{
TopSummary = 1
, BottomSummary = 2
, Error = 3
, Warning = 4
, Informational = 5
}
public struct Message
{
public string PropertyName;
public string MessageText;
public MessageTypes MessageType;
}
}


Partial View Rendering

@using MMLibrary.MMWeb.MessageHandling
@model List<Message>


Parent View Rendering

@using Music.Accounts.ViewModels
@using MMLibrary.MMWeb.MessageHandling
@model LogOn
@{
Layout = "~/Views/Shared/_Layout.cshtml";
var DisplayMessages = Model.DisplayMessages;
}
@Html.Partial("~/Views/Shared/MessageDisplay.cshtml", DisplayMessages)


ViewModel of Parent View

namespace Music.Accounts.ViewModels
{
public class LogOn
{
public string Email { get; set; }
public string Password { get; set; }
public bool RememberMe { get; set; }
public List<Message> DisplayMessages { get; set; }
}
}

Answer Source

The second parameter of Html.Partial() should not be null, otherwise it will pass Model as parameter. Иn the moment you call @Html.Partial("~/Views/Shared/MessageDisplay.cshtml", DisplayMessages) the DisplayMessages variable is null and the Model (LogOn) is passed to the partial view.