0

This is an editor for WebShop Global Settings. I needed to extend the editor with a ViewModel. It worked fine before I started but now crashes with the above error when it's invoked. What am I doing wrong?

Here's the driver:

public class WebShopSettingsPartDriver : ContentPartDriver<WebShopSettingsPart>
{
    private readonly ISiteService _siteService;
    private readonly IWebshopSettingsService _webshopSettings;
    protected override string Prefix { get { return "WebShopSettings"; } }

    private const string shapeName = "Parts_WebShopSettings_Edit";       
    private const string templateName = "Parts/WebShopSettings";    

    public WebShopSettingsPartDriver(IWebshopSettingsService webshopSettings, ISiteService siteService)
    {
        _webshopSettings = webshopSettings;
        _siteService = siteService;
    }

    protected override DriverResult Editor(WebShopSettingsPart part, dynamic shapeHelper)
    {
        var settings = _siteService.GetSiteSettings().As<WebShopSettingsPart>();

        var model = new WebShopSettingsVM
        {
            WebShopSettings = settings,
            ShippingProducts = _webshopSettings.ShippingProductRecords()
        };

        return ContentShape(shapeName,
            () => shapeHelper.EditorTemplate(TemplateName: templateName, Model: model, Prefix: Prefix)).OnGroup("WebShop");
    }
}

}

Here is the Handler:

 public class WebShopSettingsPartHandler : ContentHandler {

    public WebShopSettingsPartHandler(IRepository<WebShopSettingsRecord> repository) {
        T = NullLocalizer.Instance;

        Filters.Add(new ActivatingFilter<WebShopSettingsPart>("Site"));
        Filters.Add(StorageFilter.For(repository));
        OnGetContentItemMetadata<WebShopSettingsPart>((context, part) => context.Metadata.EditorGroupInfo.Add(new GroupInfo("WebShop")));
    }
}

And here is the first line of the View (which is in Views\EditorTemplates\Parts\WebShopSettings.cshtml):

@model Cascade.WebShop.ViewModels.WebShopSettingsVM

The Placement.ini file has the following entry:

<Place Parts_WebShopSettings_Edit="Content:0" />

Here is the ViewModel:

public class WebShopSettingsVM
{
    public IEnumerable<ShippingProductRecord> ShippingProducts{ get; set; }

    [Required]
    public int? ShippingProductRecordId { get; set; }

    public WebShopSettingsPart WebShopSettings { get; set; }

    // Expose all the properties of the Part directly on the VM

    [Required]
    public string AdministratorEmailAddress
    {
        get { return WebShopSettings.AdministratorEmailAddress; }
        set { WebShopSettings.AdministratorEmailAddress = value; }
    }
    [Required]
    public string ContinueShoppingUrl
    {
        get { return WebShopSettings.ContinueShoppingUrl; }
        set { WebShopSettings.ContinueShoppingUrl = value; }
    }
    // and so on...
}

After Bertrand's suggestion below I updated the View to:

@using Cascade.WebShop.ViewModels
@using Cascade.WebShop.Models
@{
    var vm = Model.Model as WebShopSettingsVM;
}
<fieldset>
    <legend>@T("Webshop")</legend>
    <div>
        <label for="@Html.FieldIdFor(x=>vm.AdministratorEmailAddress)">@T("Administrator email address")</label>
        @Html.TextBoxFor(x=>vm.AdministratorEmailAddress, new { @class = "textMedium" })
        @Html.ValidationMessage("AdministratorEmailAddress", "*")
...

Insights and suggestions greatly appreciated -- I simply can't see what's wrong.

4

2 回答 2

1

驱动程序的第二个副本,名称略有不同,位于“Helpers”目录中。毫不奇怪,我没有注意到这一点。'Helper' 驱动程序提供一个零件,而'正确' 驱动程序提供一个 VM。两者都被解雇了,因此无论我使用 VM 还是使用第 1 部分或其他两个驱动程序都会引发异常。

删除虚假驱动程序解决了这个问题。对不起伯特兰。

于 2013-03-23T05:21:18.240 回答
0

模型仍然是形状。删除指令,然后访问 Model.Model 以访问您的视图模型:Model 是形状,Model.Model 是形状上名为 Model 的属性。

只需将 Model.Model 转换为视图模型的类型。

于 2013-03-22T06:39:25.493 回答