1

我刚开始使用 n2cms,我不确定这个问题是与我设置它的方式有关,还是与 asp.net MVC 和 PartialViews 有关。

我使用的是 NuGet n2cms 包和 n2cms razor 支持包,但不是 n2cms Dinamico 包。

我将 DTO 传递给视图,在渲染视图时,出现错误:

传入字典的模型项的类型为“Castle.Proxies.WhatsNewItemProxy”,但该字典需要类型为“Simpletons.Models.WhatsNewDTO”的模型项

链接到我在 n2cms codeplex 上的帖子,该帖子解释了我如何使用 n2cms,基本模型和控制器的代码:https ://n2cms.codeplex.com/discussions/453163

我有一个 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>();
    }
}

我有 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
        });
    }
}

ViewModel:WhatsNewDTO

public class WhatsNewDTO
{
    public string Title { get; set; }
    public IEnumerable<WhatsNewItem> WhatsNewItems { get; set; }
}

ContentPage 视图:Views/WhatsNewPage/ Index.cshtml

@model Simpletons.Models.WhatsNewDTO

<div class="span12">
    <h2>@Model.Title</h2>
    @{Html.DroppableZone("WhatsNew").Render();}
</div>

WhatsNewPage 内部是 DroppableZone,可以在其中拖放 WhatsNewItems。

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; }
}

WhatsNewItem 的控制器:ContentPartController

[Controls(typeof(ContentPart))]
public class ContentPartController : ContentController<ContentPart>
{
    public override ActionResult Index()
    {
        return PartialView(CurrentItem.TemplateKey, CurrentItem);
    }

}

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/>
}
4

0 回答 0