Click or drag to resize

Web MVC - view model is the wrong type

When using of SOWIWeb.Foundation views and of SOWIWeb.Helper. Standard Controller and of SOWIWeb.Helper.ContentsExtensions event OnContentsToPath

Can triggers an exception message: The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies...', but this dictionary requires a model item of type '...'.

Problem

Web controller view calls the wrong partial of content (ViewBag.ContentName) so has wrong model

Reason

When puts local user defined view don't reset the event handler. So puts this content all time.

An example

C#
/// <summary>
/// Constructor without parameter
/// </summary>
public SchachtTypPosController() : base()
{
    //* add event handler for a custom content view path setting *
    SOWIWeb.Helper.ContentsExtensions.OnContentsToPath += this.OnContentsToPath;
}

/// <summary>
/// Custom Content view path: Edit
/// </summary>
/// <param name="sender">null object because it's static declaration</param>
/// <param name="e">Has view name and view path</param>
private void OnContentsToPath(object sender, EventArgsViewEnum<Contents> e)
{
    switch (e.View)
    {
        case Contents.none:
            break;
        case Contents.CreateContent:
            break;
        case Contents.DeleteContent:
            break;
        case Contents.DetailsContent:
            break;
        case Contents.EditContent:
            e.Path = "~/Views/SchachtTypPos/EditContent.cshtml";
            break;
        case Contents.IndexContentDataTables:
            break;
        default:
            break;
    }
}
Solution

When a local content to remove the event handler.

An example

C#
/// <summary>
/// Remove event handler for a custom content view path setting
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
    SOWIWeb.Helper.ContentsExtensions.OnContentsToPath -= this.OnContentsToPath;

    base.Dispose(disposing);
}
See Also