Umbraco 8 - Model Settings
# help-with-umbraco
w
Hello, I am playing around with the model settings for a BlockListItem and I'm struggling to get this working;
Copy code
@using Umbraco.Core.Models.Blocks
@inherits UmbracoViewPage<BlockListItem<TitleBlock>>

@{
    var settings = (TitleBlockSettings)Model.Settings;
    
    if(!settings.HasImageOrSubtext) {
        <p>something here</p>
    }
}

<section id="sd-wan-info" class="title block longer has-image eo">
    <div class="inner">
        <div class="text-part">
            <h1>@Model.Content.TitleHeading<br></h1>
            <div class="subtitle" style="color: #0F2C43">
                @Model.Content.TextContent
            </div>
        </div>
        <div class="image-part">
            <img width="362" height="348" src="@Model.Content.TitleBlockImage.MediaUrl()" alt="@Model.Content.BlockImageAlternativeText" class="attachment-full size-full" loading="lazy">                    
        </div>        
    </div>
</section>
Here are the settings for the block: https://prnt.sc/1nhUPgzY9N-Z The settings model has been selected in the data models section: https://prnt.sc/DQN1KzEO8CcX This is the error that I am getting: System.NullReferenceException: 'Object reference not set to an instance of an object.'
Copy code
System.NullReferenceException: 'Object reference not set to an instance of an object.'
settings was null.
I'm not sure where I am going wrong here.
m
Have you saved and published the content since adding settings?
This will also save you doing the cast 🙂
Copy code
@inherits UmbracoViewPage<BlockListItem<TitleBlock, TitleBlockSettings>>
w
Aha! That's alot cleaner! And yes, I've published the content since adding the settings. @Matt Wise How would I access the settings then? Can I do;
@if(HasImageOrSubtext){ /* code here */ }
m
Model.Settings.HasImageOrSubText as Settings is now strongly typed. As for the error if the block existed before you added the settings you need to change a setting
Its also work null checking Model.Settings in general as this will stop it erroring at least
Model.Settings?.HasImageOrSubText == true would do this
w
Yeah, I saw the problem with the if statement as soon as I posted it. A brain fart moment. Yes, definitely, null checking it is a must. I've checked it and now it's working.