XForm into Block giving error

Vote:
 

The Error is: 

The model item passed into the dictionary is of type 'Castle.Proxies.XFormBlockProxy', but this dictionary requires a model item of type 'XXX.Models.ViewModels.XFormPageViewModel'.

at System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value)
at System.Web.Mvc.ViewDataDictionary.set_Model(Object value)
at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)
at System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData)
at System.Web.Mvc.WebViewPage.set_ViewData(ViewDataDictionary value)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at EPiServer.Web.Mvc.ViewExtensions.Render[T](IView view, ViewEngineResult viewEngineResult, ViewContext context, TextWriter writer, T data)
at EPiServer.Web.Mvc.MvcContentRenderer.HandleRenderTemplateWithViewEngine(HtmlHelper helper, IContentData contentData, TemplateModel templateModel)
at EPiServer.Web.Mvc.MvcContentRenderer.Render(HtmlHelper helper, PartialRequest partialRequestHandler, IContentData contentData, TemplateModel templateModel)
at XXX.Business.Rendering.ErrorHandlingContentRenderer.Render(HtmlHelper helper, PartialRequest partialRequestHandler, IContentData contentData, TemplateModel templateModel)

Here is my code:

XFormBlock.cshtml

@using EPiServer.Web.Mvc.Html

@model XXX.Models.ViewModels.XFormPageViewModel

 

<div class="xformblock" @Html.EditAttributes(x => x.Schema)>

    @Html.ValidationSummary()

 

    @using (Html.BeginXForm(Model.Schema, new { Action = Model.ActionUri }))

    {

        Html.RenderXForm(Model.Schema);

    }

</div>

 

XFormBlock.cs

using System.ComponentModel.DataAnnotations;

using EPiServer.Core;

using EPiServer.DataAbstraction;

using EPiServer.DataAnnotations;

using EPiServer.XForms;

 

namespace GoConstruct.Models.Blocks

{

    [SiteContentType(GUID = "E6CE8FD6-DCA9-454C-B219-2C4D0DB9E17D",

       GroupName = Global.GroupNames.Phase1point5,

       DisplayName = "1.5 X-Form Block",

       Description = "Add a form to the site", Order = 37)]

    [SiteImageUrl("/Static/Gfx/blocks/form.jpg")]

    public class XFormBlock : SiteBlockData

    {

        [Display(

            GroupName = SystemTabNames.Content,

            Order = 1)]

        [CultureSpecific]

        public virtual string Heading { get; set; }

 

        [Display(

            GroupName = SystemTabNames.Content,

            Order = 2)]

        [CultureSpecific]

        public virtual XForm Schema { get; set; }

    }

}

 

XFormBlockController.cs

public class XFormBlockController : BaseBlockController<XFormBlock>

    {

      

        // GET: XFormBlock

       public override ActionResult Index(XFormBlock currentBlock)

        {

            var id = (currentBlock as IContent).ContentLink.ID;

 

            // ViewData is not automatically passed to a block controller, need to get it from TempData if it exists

            var viewDataKey = string.Format("TempViewData_{0}", id);

 

            if (this.TempData[viewDataKey] != null)

            {

                this.ViewData = (ViewDataDictionary)this.TempData[viewDataKey];

            }

 

            // Create the viewmodel

            var viewModel = new XFormPageViewModel(currentBlock, EpiServerDependencies)

            {

                Schema = currentBlock.Schema,

                Heading = currentBlock.Heading

            };

 

            var pageRouteHelper = ServiceLocator.Current.GetInstance<PageRouteHelper>();

            PageData currentPage = pageRouteHelper.Page;

 

            // Create postback url

            if (viewModel.Schema != null && currentPage != null)

            {

                var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();

                var pageUrl = urlResolver.GetVirtualPath(currentPage.ContentLink);

 

 

                var actionUrl = string.Format("{0}XFormPost/", pageUrl);

                actionUrl = UriSupport.AddQueryString(actionUrl, "XFormId", viewModel.Schema.Id.ToString());

                actionUrl = UriSupport.AddQueryString(actionUrl, "failedAction", "Failed");

                actionUrl = UriSupport.AddQueryString(actionUrl, "successAction", "Success");

                actionUrl = UriSupport.AddQueryString(actionUrl, "contentId", id.ToString());

 

                viewModel.ActionUri = actionUrl;

            }

 

            var editHints = this.ViewData.GetEditHints<XFormPageViewModel, XFormBlock>();

            editHints.AddConnection(v => v.Schema, p => p.Schema);

 

            return this.PartialView(viewModel);

        }

    }

 

XFormPageViewModel.cs

 

using EPiServer.XForms;

using XXX.Models.Blocks;

using XXX.Models.ViewModels.Base;

using JonDJones.Com.Core;

public class XFormPageViewModel : BlockViewModel<XFormBlock>

    {

        public XFormPageViewModel(XFormBlock currentBlock, IEpiServerDependencies epiServerDependencies)

           : base(currentBlock, epiServerDependencies)

        {

 

        }

        public XForm Schema { get; set; }

 

        public string ActionUri { get; set; }

        public string Heading { get; set; }

    }

 

Can you let me know if you can see where I am going wrong?

 

Many thanks

Jon

#199894
Edited, Dec 17, 2018 16:48
Vote:
 

Hi Jon,

With a content block template, if you use default block controller then you only need to create a  partial view template with name convension {BlockTypeName}.cshtml and the view model type must be that block type. If you create your own controller then you need to point to correct partial view template. In your example, there are 2 ways to fix your problem:

1. Change this line 

 return this.PartialView(viewModel);

to

 return this.PartialView("XFormBlock",viewModel);

2. Change view name and create parent folder as default MVC like: Views/XFormBlock/Index.cshtml

#199925
Edited, Dec 18, 2018 2:30
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.