ViewComponent-less local block property not working with PropertyFor


Solution is recently upgraded and we are working through the templates...

It has a simple block type with string and Url properties. It has a View file but no ViewComponent class.

It is used in another block added as local block property, and in that block's view it is called using Html.PropertyFor(x => m.CurrentBlock.SimpleBlockTypeProperty)

We then get this error:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'SimpleBlock_DynamicProxy', but this ViewDataDictionary instance requires a model item of type 'APageViewModel'.

This error is without any custom property renderer.

Anyone solved this or have some pointers?

Nov 16, 2022 11:00

Hi Johan,

Are you using there ContentArea?

In my current project we had simillar issue and ve've resolved it by explicit cast to block type.
Example code:

IEnumerable<ContentAreaItem> contentAreaItems = Enumerable.Empty<ContentAreaItem>(); // your content area items
foreach (var block in contentAreaItems.Select(c => c.GetContent() as IContentData))
	switch (block)
		case BlockTypeA blockA:
			@Html.PropertyFor(m => blockA)
		case BlockTypeB blockB:
			@Html.PropertyFor(m => blockB)
		case null:
			<div>Block not handled.</div>
Nov 16, 2022 16:02
Johan Kronberg - Nov 16, 2022 16:05
No, this error seems to only when using PropertyFor on a "local block".

We have a temporary fix that seems to work as before but not using PropertyFor but instead calling the View directly using:

@await Html.PartialAsync("SimpleBlock", Model.CurrentBlock.SimpleBlockTypeProperty)

// Or with extra view data...
@await Html.PartialAsync("SimpleBlock", Model.CurrentBlock.SimpleBlockTypeProperty, new ViewDataDictionary(ViewData) { { "cssClass", "some-class" } })
Nov 16, 2022 16:09

I recently ran into somewhat the same issue - I could not use Html.PropertyFor(x => x.Block) where Block is a actual block instance (rather than a content area or a content link). In my case, I had a dedicated view component for the block, so I just did as below. The error message is very misleading though.

@(await Component.InvokeAsync<SimpleBlockComponent>(new { currentContent = simpleBlock }))

Honestly, I think your solution is fine.

Edited, Nov 16, 2022 17:04
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.