Trying to create a simple DocGridEditor based on something we used to do in Umbraco 7
Simple Button Element
I am currently getting this error
Umbraco.Web.Mvc.ModelBindingException: Cannot bind source type
Test.Infrastructure.DocumentTypes.GeneratedModels.Button to model type
Umbraco.Core.Models.PublishedContent.IPublishedContent.
at Umbraco.Web.Mvc.ContentModelBinder.ThrowModelBindingException(Boolean sourceContent, Boolean modelContent, Type sourceType, Type modelType) in d:\a\1\s\src\Umbraco.Web\Mvc\ContentModelBinder.cs:line 156
at Umbraco.Web.Mvc.ContentModelBinder.BindModel(Object source, Type modelType) in d:\a\1\s\src\Umbraco.Web\Mvc\ContentModelBinder.cs:line 119
at Umbraco.Web.Mvc.UmbracoViewPage`1.SetViewData(ViewDataDictionary viewData) in d:\a\1\s\src\Umbraco.Web\Mvc\UmbracoViewPageOfTModel.cs:line 152
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Web.Mvc.ProfilingView.Render(ViewContext viewContext, TextWriter writer) in d:\a\1\s\src\Umbraco.Web\Mvc\ProfilingView.cs:line 25
at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at Our.Umbraco.DocTypeGridEditor.Web.Extensions.HtmlHelperExtensions.RenderDocTypeGridEditorItem(HtmlHelper helper, IPublishedElement content, String editorAlias, String viewPath, String previewViewPath, Boolean isPreview)
at ASP._Page_app_plugins_doctypegrideditor_render_DocTypeGridEditor_cshtml.Execute() in C:\Projects\Test\src\Presentation\Test.Web\app_plugins\doctypegrideditor\render\DocTypeGridEditor.cshtml:line 28
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Web.Mvc.ProfilingView.Render(ViewContext viewContext, TextWriter writer) in d:\a\1\s\src\Umbraco.Web\Mvc\ProfilingView.cs:line 25
at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model)
at ASP._Page_Views_Partials_grid_editors_Base_cshtml.Execute() in C:\Projects\Test\src\Presentation\Test.Web\Views\Partials\grid\editors\Base.cshtml:line 20
public class ButtonController : DocTypeGridEditorSurfaceController
{
public ActionResult RenderPartial(Button content)
{
var model = new ButtonModel(content)
return PartialView("~/Views/Partials/Blocks/Button.cshtml", model);
}
}
ButtonModel
Derives from PublishedElementModel
Generated Button Model
namespace Test.DocumentTypes.GeneratedModels
{
/// <summary>Button</summary>
[PublishedModel("button")]
public partial class Button : PublishedElementModel
{
// helpers
#pragma warning disable 0109 // new is redundant
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
public new const string ModelTypeAlias = "button";
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
public new const PublishedItemType ModelItemType = PublishedItemType.Content;
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
public new static IPublishedContentType GetModelContentType()
=> PublishedModelUtility.GetModelContentType(ModelItemType, ModelTypeAlias);
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
public static IPublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<Button, TValue>> selector)
=> PublishedModelUtility.GetModelPropertyType(GetModelContentType(), selector);
#pragma warning restore 0109
// ctor
public Button(IPublishedElement content)
: base(content)
{ }
// properties
///<summary>
/// Button Link
///</summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
[ImplementPropertyType("buttonLink")]
public Umbraco.Web.Models.Link ButtonLink => this.Value<Umbraco.Web.Models.Link>("buttonLink");
///<summary>
/// Button Text
///</summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
[ImplementPropertyType("buttonText")]
public string ButtonText => this.Value<string>("buttonText");
}
}
Had to do the following changes to get it working.
Need to set the action result the same as the alias, so Button in this instance and prefix with surface controller. Thanks to Phil at Hifi for his input
public class ButtonSurfaceController : DocTypeGridEditorSurfaceController
{
public ActionResult Button()
{
var model = new ButtonModel(this.Model as Button);
return PartialView("~/Views/Partials/Button.cshtml", model);
}
}
public class ButtonModel
{
public ButtonModel(Button button)
{
if(button != null)
{
ButtonLink = button.Link.Url;
ButtonText = button.Link.Name;
}
}
public string ButtonText { get; set; }
public string ButtonLink { get; set; }
}
Issue with Custom Editor
Trying to create a simple DocGridEditor based on something we used to do in Umbraco 7
Simple Button Element
I am currently getting this error
grid.editors.config.js
Grid/Editors/DocTypeGridEditor/Button
Grid/Editors/DocTypeGridEditor/_DefaultDocTypeGridEditorLayout
Grid/Editors/DocTypeGridEditor/_DocTypeGridEditorLayout
}
Views/Partials/Button
ButtonController
ButtonModel Derives from PublishedElementModel
Generated Button Model
Had to do the following changes to get it working.
Need to set the action result the same as the alias, so Button in this instance and prefix with surface controller. Thanks to Phil at Hifi for his input
is working on a reply...