Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alistair Graham 22 posts 62 karma points
    Oct 01, 2019 @ 16:20
    Alistair Graham
    0

    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

    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
    

    grid.editors.config.js

    {
        "name": "Button",
        "alias": "button",
        "view": "/App_Plugins/DocTypeGridEditor/Views/doctypegrideditor.html",
        "render": "/App_Plugins/DocTypeGridEditor/Render/DocTypeGridEditor.cshtml",
        "icon": "icon-link color-blue",
        "config": {
            "allowedDocTypes": ["button"],
            "nameTemplate": "",
            "enablePreview": true,
            "viewPath": "/Views/Partials/Grid/Editors/DocTypeGridEditor/",
            "previewViewPath": "/Views/Partials/Grid/Editors/DocTypeGridEditor/",
            "previewCssFilePath": "",
            "previewJsFilePath": ""
        }
    }
    

    Grid/Editors/DocTypeGridEditor/Button

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
    Layout = "_DefaultDocTypeGridEditorLayout.cshtml";
    }
    

    Grid/Editors/DocTypeGridEditor/_DefaultDocTypeGridEditorLayout

    @inherits UmbracoViewPage<IPublishedElement>
    @{
        Layout = "_DocTypeGridEditorLayout.cshtml";
       }
     @Html.Action("RenderPartial", Model.ContentType.Alias, new { content = Model })@RenderBody()
    

    Grid/Editors/DocTypeGridEditor/_DocTypeGridEditorLayout

    @using System.Web.Configuration
    @using Test.Infrastructure.DocumentTypes.GeneratedModels
    
    @using Umbraco.Core.Logging
    @inherits UmbracoViewPage<IPublishedElement>
    
    @{
    var compilationSection = System.Configuration.ConfigurationManager
        .GetSection(@"system.web/compilation") as CompilationSection;
    var isDebugEnabled = compilationSection?.Debug
                         ?? false;
    }
    
    @if (Request.QueryString["dtgePreview"] == "1")
    {
    
    var blockCssScope = Model.ContentType.Alias;
    
    <div class="umbraco-backend-scope @blockCssScope">
        @RenderBody()
    </div>
    
    <script>
        // unwraps the noscripts to display the images.        
        $(".umbraco-backend-scope.@blockCssScope noscript")
            .each(function(){
                var noScript = $(this);
                noScript.replaceWith(noScript.text());
            });
    
        // Set the iFrame src as the data-src, this was done to allow cookiebot to work properly!
        $("[email protected] iFrame")
            .each(function(){
                var iFrame = $(this);
                var dataSrc = iFrame.attr("data-src");
                if(dataSrc)
                    iFrame.attr("src", dataSrc);
            });
    </script>
    }
    else
    {
    try
    {
        @RenderBody()
    }
    catch (Exception e)
    {
    
    
        if (isDebugEnabled)
        {
            throw;
        }
    }
    

    }

    Views/Partials/Button

    @model Test.Feature.Blocks.Button.ButtonModel
    
    @{
         var test = Model.CurrentPage as Button;
    
      }
    
     @if (test.ButtonLink !=null)
     {
    <div class="section full-width">
        <div class="container-fluid">
            <div class="row">
                <div class="general-link-promo-container">
                    @Html.SafeAnchor(test.ButtonLink, innerText:test.ButtonText, @class: "btn")
                </div>
            </div>
        </div>
    </div>
    }
    

    ButtonController

    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");
    }
    }
    
  • Alistair Graham 22 posts 62 karma points
    Oct 07, 2019 @ 10:49
    Alistair Graham
    100

    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; }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft