InvalidCastException error from returning a custom...
# help-with-umbraco
j
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:
Copy code
public class ProductTypeModel : Productpage
    {
        public ProductTypeModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback) : base(content, publishedValueFallback)
        {
        }

        //Custom properties
        public string ProductType
    }
And Controller action:
Copy code
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!
Also if i add IPublishedContent to the model:
Copy code
public IPublishedContent? Content { get; set; }
And dont send the content from the controller i get the modelbinding error:
Copy code
ModelBindingException: Cannot bind source type WebApp.Models.ProductTypeModel to model type Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent.
Maybe that clarifies something 🙂
26 Views