0

これは、WebShopグローバル設定のエディターです。ViewModelでエディターを拡張する必要がありました。開始する前は正常に機能していましたが、呼び出されると上記のエラーでクラッシュします。私は何が間違っているのですか?

ドライバーは次のとおりです。

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

}

ハンドラーは次のとおりです。

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

そして、これがビューの最初の行です(Views \ EditorTemplates \ Parts \ WebShopSettings.cshtmlにあります):

@model Cascade.WebShop.ViewModels.WebShopSettingsVM

Placement.iniファイルには次のエントリがあります。

<Place Parts_WebShopSettings_Edit="Content:0" />

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

以下のBertrandの提案の後、ビューを次のように更新しました。

@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", "*")
...

洞察と提案は非常に高く評価されています-私は単に何が悪いのかわかりません。

4

2 に答える 2

1

わずかに異なる名前のドライバーの2番目のコピーが、「Helpers」ディレクトリにありました。当然のことながら、私はこれに気づきませんでした。「ヘルパー」ドライバーはパーツを提供し、「適切な」ドライバーはVMを提供していました。両方とも起動されていたため、VMを使用したか、2つのドライバーのパート1または他を使用したかにかかわらず、例外がスローされました。

スプリアスドライバーを削除すると、問題が修正されました。ベルトランごめんなさい。

于 2013-03-23T05:21:18.240 に答える
0

モデルはまだ形です。ディレクティブを削除し、Model.Modelにアクセスしてビューモデルにアクセスします。Modelは図形であり、Model.Modelは図形上にあるModelという名前のプロパティです。

Model.Modelをビューモデルのタイプにキャストするだけです。

于 2013-03-22T06:39:25.493 に答える