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.