Copied to clipboard

Flag this post as spam?

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


  • swathi 3 posts 94 karma points
    Jul 19, 2018 @ 09:34
    swathi
    0

    Surface controller model binding

    Hi,

    Thanks in advance for your help.

    I am using Umbraco 7.11.1, I created custom controller in visual studio, Which inherits SurfaceController. And inherits UmbracoViewPage in view.

    Here is the sample code,

    Model:

     public class CompanyModel {...}
    

    View:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @inherits UmbracoViewPage<Business.CompanyModel>
    
    @using (Html.BeginForm("Company", "CoverSurface", FormMethod.Post, new { @role = "form",  @novalidate = "novalidate" }))  ...
    

    Controller:

     public class CoverSurfaceController : SurfaceController    {
    public ActionResult Company()
            {
               var model = new CompanyModel()
                    {
                        //call to generate model
                    };
    
                    return View(model);
            }
    }
    

    When i run in debug, Routing happens fine and I can see the control hitting right action and view. And the end of view debug, its throwing exception as follows :

    Cannot bind source type Business.CompanyModel to model type Umbraco.Web.Models.RenderModel.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: Umbraco.Web.Mvc.ModelBindingException: Cannot bind source type Business.CompanyModel to model type Umbraco.Web.Models.RenderModel.
    
    [ModelBindingException: Cannot bind source type Business.CompanyModel to model type Umbraco.Web.Models.RenderModel.]
       Umbraco.Web.Mvc.RenderModelBinder.ThrowModelBindingException(Boolean sourceContent, Boolean modelContent, Type sourceType, Type modelType) +424
       Umbraco.Web.Mvc.RenderModelBinder.BindModel(Object source, Type modelType, CultureInfo culture) +502
       Umbraco.Web.Mvc.UmbracoViewPage`1.SetViewData(ViewDataDictionary viewData) +180
       System.Web.Mvc.WebViewPage.ConfigurePage(WebPageBase parentPage) +57
       System.Web.WebPages.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer) +218
       System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +10
       System.Web.WebPages.WebPageBase.Write(HelperResult result) +80
       System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +63
       System.Web.WebPages.WebPageBase.PopContext() +237
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +98
       System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
       System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
       Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) +113
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +290
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
       System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
       System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
       System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
       System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
       System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
       System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
       System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
       System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
       System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9987157
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
    
  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jul 19, 2018 @ 09:47
    Nik
    0

    Hi Swathi,

    The issue you are seeing is caused by your view code.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @inherits UmbracoViewPage<Business.CompanyModel>
    

    You are inheriting twice, if you remove the first inherits line it should solve the issue for you.

    Thanks,

    Nik :-)

  • swathi 3 posts 94 karma points
    Jul 19, 2018 @ 09:57
    swathi
    100

    Hi Nik,

    Thank you so much for reply.

    @inherits UmbracoViewPage

    As you mentioned I removed the first inherits, still getting the same exception.

    Is there anything else i am missing?

    Thanks, Swathi.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jul 19, 2018 @ 10:02
    Nik
    0

    Hmm, does your view have anything like :

    Layout = "Some other view"
    

    If so, that could be causing it. Surface controllers are usually for returning partial views rather than full views so your controller should do

    Return PartialView() //I can't recall the exact syntax here sorry
    

    instead of

    Return View() 
    

    You also shouldn't need to be setting a Layout in the partial :-)

    Nik

  • swathi 3 posts 94 karma points
    Jul 19, 2018 @ 10:07
    swathi
    1

    Thank you so much Nik, Working fine now.I wasn't setting layout but was returning View(). Changing it to partial view fixed it.

    Thanks, Swathi.

Please Sign in or register to post replies

Write your reply to:

Draft