I'm just starting to use n2cms, and I'm not sure if this issue is with the way I've set it up, or with asp.net MVC and PartialViews.
I'm using the NuGet n2cms package, and the n2cms razor support package, but not the n2cms Dinamico package.
I pass a DTO to a view, and when it comes to rendering the view, I get the error:
The model item passed into the dictionary is of type 'Castle.Proxies.WhatsNewItemProxy', but this dictionary requires a model item of type 'Simpletons.Models.WhatsNewDTO
Link to my post on n2cms codeplex that explains how I'm using n2cms, code for Base models and controllers: https://n2cms.codeplex.com/discussions/453163
I have a ContentPage : WhatsNewPage
[PageDefinition("Whats New Page", IconUrl = "~/N2/Resources/icons/page_world.png")]
[WithEditableTitle]
[WithEditableName]
public class WhatsNewPage : PageModelBase
{
public virtual IEnumerable<WhatsNewItem> GetWhatsNewItems()
{
return Find.CurrentPage.Children.Cast<WhatsNewItem>();
}
}
I have the Controller for the WhatsNewPage: WhatsNewPageController
[Controls(typeof(WhatsNewPage))]
public class WhatsNewPageController : ContentController<WhatsNewPage>
{
public override ActionResult Index()
{
var items = CurrentItem.GetWhatsNewItems();
return View("Index", new WhatsNewDTO
{
Title = CurrentItem.Title,
WhatsNewItems = items
});
}
}
The ViewModel: WhatsNewDTO
public class WhatsNewDTO
{
public string Title { get; set; }
public IEnumerable<WhatsNewItem> WhatsNewItems { get; set; }
}
The ContentPage View: Views/WhatsNewPage/Index.cshtml
@model Simpletons.Models.WhatsNewDTO
<div class="span12">
<h2>@Model.Title</h2>
@{Html.DroppableZone("WhatsNew").Render();}
</div>
Inside the WhatsNewPage is the DroppableZone where WhatsNewItems can be drag/dropped.
WhatsNewItem
[PartDefinition("WhatsNewItem", IconUrl = "~/N2/Resources/icons/layout.png")]
public class WhatsNewItem : ContentPart
{
public override string TemplateKey
{
get
{
return "WhatsNew";
}
}
[EditableText("Brand", 100)]
public virtual string Brand { get; set; }
[EditableText(Title = "Heading", SortOrder = 200)]
public virtual string Heading { get; set; }
[EditableText(Title = "Heading date", SortOrder = 300)]
public virtual string HeadingDate { get; set; }
[EditableImage("What's new image", 400)]
public virtual string Image { get; set; }
[EditableText("Image caption", 500)]
public virtual string ImageCaption { get; set; }
[EditableFreeTextArea("What's new text", 600)]
public virtual string Text { get; set; }
}
The Controller for WhatsNewItem: ContentPartController
[Controls(typeof(ContentPart))]
public class ContentPartController : ContentController<ContentPart>
{
public override ActionResult Index()
{
return PartialView(CurrentItem.TemplateKey, CurrentItem);
}
}
The View for WhatsNewItem: Views/Shared/WhatsNew.cshtml
@model Simpletons.Models.WhatsNewDTO
@foreach (var detail in @Model.WhatsNewItems)
{
<div class="whatsnew-detail clearfix">
<img src="@detail.Image" class="whatsnew-image pull-left"/>
<p class="whatsnew-heading">
@detail.Heading
</p>
<p class="whatsnew-date">
@detail.HeadingDate
</p>
<p class="whatsnew-text">
@detail.Text.ToHtmlString()
</p>
</div>
<hr/>
}