Hi there,
Every time I add a new field to a block, old pages crashes with this error.
Not just in frontend but in manager edit page also.
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Areas_Manager_Views_Shared_EditorTemplates_GalleryBlock.ExecuteAsync() in GalleryBlock.cshtml
<div class="form-group">
<label>Image #16</label>
@Html.EditorFor(m => m.Image16) *** OLD FIELD JUST FINE ***
</div>
<div class="form-group">
<label>Image #17</label>
@Html.EditorFor(m => m.Image17) *** NEW FIELD NOT SO GOOD ***
</div>
<div class="form-group">
My come-up crazy solution for this;
Of course all my old data have to be removed and need to re-add also.
Hi there. When a new block is created it is created with the method IContentService.CreateBlock. This methods instantiates all properties that are inherited from IField. When an existing block is loaded it is just deserialized which means that non-existing fields will be null. Performing an automatic initialization of every null field on load would be a negative performance hit as this is a runtime operation and not an admin operation.
To solve this problem (without loosing your data), just instantiate your fields in your class, like so.
public class MyBlock : Block
{
public StringField Title { get; set; } = new StringField();
public TextField Body { get; set; } = new TextField();
}
Regards
Thanks!
Most helpful comment
Hi there. When a new block is created it is created with the method
IContentService.CreateBlock. This methods instantiates all properties that are inherited fromIField. When an existing block is loaded it is just deserialized which means that non-existing fields will benull. Performing an automatic initialization of every null field on load would be a negative performance hit as this is a runtime operation and not an admin operation.To solve this problem (without loosing your data), just instantiate your fields in your class, like so.
Regards