Copied to clipboard

Flag this post as spam?

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


  • Elliott Quick 8 posts 80 karma points
    May 11, 2016 @ 16:13
    Elliott Quick
    0

    Cannot bind source type Umbraco.Web.Models.PartialViewMacroModel

    I am trying to create a macro so that I can easily insert my ContactForm.cshtml partial page anywhere in a content section.

    No matter how much I try the result is usually the same, either I get stuck with the page displaying "Macro alias: InsertContactForm" where the partial page should render.

    I am using a seperate webservice to process the request, so I have javascript that validates and sends the information. I am not using a controller or model.

    I see this error:

     Error loading Partial View (file: ~/Views/MacroPartials/InsertContactForm.cshtml). Exception: Umbraco.Web.Mvc.ModelBindingException: Cannot bind source type Umbraco.Web.Models.PartialViewMacroModel to model type Umbraco.Web.Models.RenderModel.
       at Umbraco.Web.Mvc.RenderModelBinder.ThrowModelBindingException(Boolean sourceContent, Boolean modelContent, Type sourceType, Type modelType)
       at Umbraco.Web.Mvc.RenderModelBinder.BindModel(Object source, Type modelType, CultureInfo culture)
       at Umbraco.Web.Mvc.UmbracoViewPage`1.SetViewData(ViewDataDictionary viewData)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
       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)
       at ASP._Page_Views_MacroPartials_InsertContactForm_cshtml.Execute() in d:\home\site\wwwroot\Views\MacroPartials\InsertContactForm.cshtml:line 1
       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.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
       at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult)
       at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content)
       at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode node)
       at umbraco.macro.LoadPartialViewMacro(MacroModel macro)
       at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)
    

    My InsertContactForm.cshtml (under macro partials)

            @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
            @Html.Partial("ContactForm")
    

    My ContactForm.cshtml (under partials) if it helps

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
        <form id="contact" role="form" class="" data-submit="" method="POST">
              @Html.AntiForgeryToken()
                <div class="form-group">
                        <label class="control-label">Name</label>
                        <input type="text" id="nameTB" class="form-control" name="name" placeholder="Ex. Elliott/Shadeslayor" />
                    </div>
                    <div class="form-group">
                        <label class="control-label">Email</label>
                        <input type="email" id="emailTB" class="form-control" name="email" placeholder="Ex. [email protected]" />
                    </div>
                    <div class="form-group">
                        <label class="control-label">Message</label>
                        <textarea class="form-control" id="bodyTB" name="body" placeholder="Ex. The pages aren't scrolling correctly on my iPhone"></textarea>
                    </div>
    
                    <div class="form-group">
                        <label class="control-label" id="captchaOperation"></label>
                        <input type="text" class="form-control" name="captcha" placeholder="Prove you are not a robot" />
                    </div>
    
                    <button id="sendBTN" type="submit" class="btn btn-primary push-right" name="Send">
                        Send
                    </button>
        </form>
    
  • Elliott Quick 8 posts 80 karma points
    May 11, 2016 @ 16:49
    Elliott Quick
    0

    I am still learning, perhaps macro's are not meant to be used this way. Regardless I figured that the point of a macro was to be reusable, and in this case the contact form is not intended to be reused.

    So what I did instead was delete the macro stuff and simply created a template and added it to the optional templates on my landing page document type and selected the new template for use on that specific page. In the template I included an Html.Partial which added my partial .cshtml page to the end of the document.

  • Elliott Brown 41 posts 211 karma points
    Sep 12, 2018 @ 12:27
    Elliott Brown
    0

    A late reply, but hopefully helpful for anyone else who experiences this problem.

    Instead of using @inherits Umbraco.Web.Mvc.UmbracoTemplatePage, as this is what's causing the conflict between this and Umbraco.Web.Models.PartialViewMacroModel

    If you leave out the 'inherits' bit all together, you can initialise

    var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    

    and call

    umbracoHelper.TypedContent(xxxx) instead of Umbraco.TypedContent(xxxx) ... for instance

    umbracoHelper gives you access to all the same functionality as you get by inheriting the model through @inherits

Please Sign in or register to post replies

Write your reply to:

Draft