将ViewModel传递给View会产生Castle.Proxies.SomeModel而不是传递的ViewModel

时间:2013-08-14 12:20:13

标签: asp.net-mvc n2cms

我刚刚开始使用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内部是可以拖放WhatsNewItems的DroppableZone。

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的视图:观点/共享/ 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/>
}

0 个答案:

没有答案