Cannot convert type 'Umbraco.Core.Dynamics.DynamicNull' to 'Umbraco.....' - Umbraco v6

2.2k views Asked by At

I've looked at a couple of similar errors regarding IPublishedContent (yes I know this is a different object than what I'm using) in Umbraco, and they've all said it's been fixed since 4.11.7 - however, I'm using v6 so obviously that's not the case.

My code worked until I threw in a coalesce:

Worked:

DynamicPublishedContent countryFolder = Umbraco.Media(CurrentPage.GetProperty("contestMediaFolder").Value.ToString());

No longer works:

DynamicPublishedContent countryFolder = !string.IsNullOrEmpty(contestFolder)
                                             ? Umbraco.Media(CurrentPage.GetProperty("contestMediaFolder").Value.ToString())
                                             : Umbraco.Media(contestFolder);

Cannot convert type 'Umbraco.Core.Dynamics.DynamicNull' to 'Umbraco.Web.Models.DynamicPublishedContent'

I have (2) controllers that are calling the same code, one is a controller that is hit on page load, the other is an API controller. This is the reason I'm passing in the variable, contestFolder, which is a string, since this method returns a list. In my method call, I'm also passing in string.Empty, which would run the first line.

I've tried:

  • Replacing the strongly typed object, DynamicPublishedContent with var.
  • Passing in an int rather than a string to the method (contestFolder would be an int).
  • Casting both returns on the Umbraco.Media() to DynamicPublishedContent
  • Instead of using DynamicPublishedContent, use IPublishedContent object.
1

There are 1 answers

0
Rob Scott On BEST ANSWER

I switched out how I retrieved the folder. I singled out the reason, and it's b/c if regardless if that 2nd coalesce hits, Umbraco attempts to retrieve the folder, and if it's null, it returns that error,

Cannot convert type 'Umbraco.Core.Dynamics.DynamicNull' to 'Umbraco.Web.Models.DynamicPublishedContent'

Therefore, you HAVE to pass an actual media id (that's valid) to the Umbraco.Media() method, or you'll have this error returned to you.

public override ActionResult Index(RenderModel model)
{
    var entries = this.GetContestEntries(this.GetCountryFolder(CurrentPage.GetProperty("contestMediaFolder").Value.ToString()));

    return base.Index(model);
}

public DynamicPublishedContent GetCountryFolder(string countryFolder)
{
    return (DynamicPublishedContent)Umbraco.Media(countryFolder);
}

public List<WAR2015ContestModel> GetContestEntries(DynamicPublishedContent countryFolder)
{
     .....  // code omitted
}