InvalidCastException error from returning a custom model to a view
j

Jacob

10 months ago
Hi, So i have a problem with sending my custom model to a view. I've tried different approaches, but either end up geeting the InvalidCastException: `InvalidCastException: Unable to cast object of type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary`1`[Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent]' to type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary1[MYMODEL]'` I've tried by inheriting from UmbracoPageController, IVirtualPageController with the FindContent function and from the RenderController, but i cant get it to work. My route is is hitting the controller action. and sending it on the view, but something in the view or model. is not configured correctly. This is my model:
public class ProductTypeModel : Productpage
    {
        public ProductTypeModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback) : base(content, publishedValueFallback)
        {
        }

        //Custom properties
        public string ProductType
    }
And Controller action:
public IActionResult Product(string producttype, string product, ProductType page)
{

    if (CurrentPage is not IPublishedContent currentPage)
    {
        return NotFound();
    }

    var model = new ProductTypeModel(currentPage, _publishedValueFallback)
    {
        ProductType = producttype?.ToLower() ?? string.Empty,
        Product = product?.ToLower() ?? string.Empty,
        Page = page,
        ProductUrl = HttpContext.Request.Path
    };
    var preparedModel = mProductControllerService.PrepareProductModel(model);

    if (preparedModel.Success)
    {
        return View("~/Views/Product/Productpage.cshtml", preparedModel.Value);
    }
    return NotFound();


}
Thanks in advance!