can you check what value you have on block's view line "@model ..."? this is classical error when you pass block instance to the view that expects different model.
Hi Eric
Do the action methods for those blocks return View()
instead of PartialView()
.
This would cause the layout page to be called on the blocks, which it shouldn't.
Hi,
Can you please post the code sample here so that we can have look on this.
I have several simple blocks. They all inherit from the same base case, SiteBlockData and use the same controller to render. Some work and some do not and I can't see the difference. All are dropped into the same content zone.
Here is the code
NON-WORKING MODEL
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Find.Cms;
using MTA_Episerver.Models.Media;
namespace MTA_Episerver.Models.Blocks.Article
{
[ContentType(DisplayName = "Article Podcast Block",
GUID = "5e023e5f-dcdf-4d22-a9f2-bb1df6c78036",
GroupName = Global.GroupNames.ArticleBlocks,
Description = "Used to display a podcast player on articles pages")]
[IndexInContentAreas]
public class PodcastBlock : SiteBlockData
{
[Display(
Name = "Podcast",
GroupName = SystemTabNames.Content,
Order = 10)]
[Required]
[AllowedTypes(typeof(Audio))]
public virtual ContentReference Podcast { get; set; }
}
}
NON_WORKING VIEW
@using EPiServer.Web.Mvc.Html
@model MTA_Episerver.Models.Blocks.Article.PodcastBlock
<div class="audio_bar">
<audio controls="">
<source src="@Url.ContentUrl(Model.Podcast)" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
WORKING MODEL
using System.ComponentModel.DataAnnotations;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Find.Cms;
namespace MTA_Episerver.Models.Blocks.Article
{
[ContentType(DisplayName = "Article Quote Block",
GUID = "91257963-726c-49dc-9a51-eb3c70315dce",
GroupName = Global.GroupNames.ArticleBlocks,
Description = "Displays a block quote on articles pages")]
[IndexInContentAreas]
public class ArticleQuoteBlock : SiteBlockData
{
[Display(
Name = "Quote",
GroupName = SystemTabNames.Content,
Order = 10)]
[Required]
public virtual string Quote { get; set; }
[Display(
Name = "Author Line",
GroupName = SystemTabNames.Content,
Order = 20)]
public virtual string AuthorLine { get; set; }
}
}
WORKING VIEW
@using EPiServer.Web.Mvc.Html
@model MTA_Episerver.Models.Blocks.Article.ArticleQuoteBlock
@{
Layout = null;
}
<blockquote>
<div class="sharing">
<div class="share-icon"></div>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
<a class="addthis_button_linkedin" addthis:url="https://addthis.com" addthis:title="THE TITLE" addthis:media="THE IMAGE" target="_blank" title="LinkedIn" href="#">
<i class="fab fa-linkedin" aria-hidden="true"></i>
</a>
<a class="addthis_button_twitter" addthis:url="https://addthis.com" addthis:title="THE TITLE" addthis:media="THE IMAGE" title="Twitter" href="#">
<i class="fab fa-twitter" aria-hidden="true"></i>
</a>
<a class="addthis_button_email" target="_blank" title="Email" href="#">
<i class="far fa-envelope"></i>
</a>
<a class="addthis_button_instagram" addthis:url="https://addthis.com" addthis:title="THE TITLE" addthis:media="THE IMAGE" target="_blank" title="Instagram" href="#">
<i class="fab fa-instagram" aria-hidden="true"></i>
</a>
<a class="addthis_button_facebook" addthis:url="https://addthis.com" addthis:title="THE TITLE" addthis:media="THE IMAGE" title="Facebook" href="#">
<i class="fab fa-facebook" aria-hidden="true"></i>
</a>
<div class="atclear"></div>
</div>
</div>
@Html.PropertyFor(m => m.Quote)
@if (!string.IsNullOrEmpty(Model.AuthorLine))
{
<span class="author">@Html.PropertyFor(m => m.AuthorLine)</span>
}
</blockquote>
Hi Eric
I still believe your error relates to your block controllers. Can you share those, too?
In your working view, you have Layout = null
, which you don't have on your non-working view. Try adding that to the non-working view and let me know the result.
That being said, setting Layout = null
is a quick fix. As I wrote previously, you should rather call PartialView()
instead of View()
in the controller. The you don't need to override the Layout
property.
Hi Eric,
Yes, stefan is correct.
Give a try to Layout=null and also put the controller code here if Layout=null not works.
SOLVED
Brilliant. That was the problem. What a rookie mistake. I was so focused on the model that I didn't even notice the missing Layout statement. Thanks for your help.
btw, partial views (blocks) should not care about layout (hence - they should not set Layout to null). If you can - please post controller code..
off-topic :D
Hi Eric,
I think there should be a button visible to you were you can mark Stefans answer as 'accepted answer' and the forum should automagically mark this thread 'solved' (just assuming you have just edited the thread title yourself).
Episerver version v11.12
I have created some very simple blocks that get added to a page in a standard ContentArea. Some of the blocks work, others give the error message below. I am NOT using eCommerce. I've tried recreating the blocks with different code names and GUIDs and versioned the site in the AssemblyInfo.cs file. Nothing seems to work. Any ideas?
Exception message:
The model item passed into the dictionary is of type 'Castle.Proxies.TwoColumnBlockProxy', but this dictionary requires a model item of type 'MTA_Episerver.Models.ViewModels.IPageViewModel`1[MTA_Episerver.Models.Pages.SitePageData]'.
Exception stack trace:
at System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) at System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) at System.Web.Mvc.WebViewPage.ConfigurePage(WebPageBase parentPage) at System.Web.WebPages.WebPageBase.