Copied to clipboard

Flag this post as spam?

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


  • Alex 4 posts 74 karma points
    Jan 17, 2019 @ 11:44
    Alex
    0

    Custom Models and Controllers

    Hello,

    My name is Alex, it's nice to join the community. I'm a graphic designer in Plymouth UK that also does some light Umbraco Development work on the side. I say light because most of my working knowledge of Umbraco (and c# for that matter) is hacked together from different places and tutorials.

    I've come up against a problem when trying to follow the documentation here.

    When trying to view Profile.cshtml through the site I get the error:

    No parameterless constructor defined for this object. Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

    I've put the full stack trace at the bottom.

    My intention is to have Profile.csthml use a custom model that inherits USNBaseViewModel. So that Profile template can be used with the layout components in the uSkinned theme.

    I'm really stumped as to why this doesn't work, my model has a constructor that follows the documentation and other models in my own project. Why do those models when used by the site not return 'No parameterless constructor defined'? Any help or advice on how to go about resolving it would be great! Also are there any tips of beginner guides to understanding the stack trace outputs?

    Many thanks for any and all help/pointers/clues. :)

    My Code:

    WtdMemberProfileController.cs - Controller

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Core.Models;
    
    namespace gy_ccg_local.App_Code.WTDControllers
    {
        public class WtdMemberProfileController : Umbraco.Web.Mvc.RenderMvcController
        {
            // GET: WtdMemberProfile
            public ActionResult Profile(WTDModels.WtdMemberProfileModel model)
            {
                var _profileModel = new WTDModels.WtdMemberProfileModel((IPublishedContent)model);
    
                //return CurrentTemplate(_profileModel);
    
                return CurrentTemplate(_profileModel);
            }
        }
    }
    

    WtdMemberProfile.cs - Model

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core.Models;
    using Umbraco.Web.Models;
    using USN.USNModels;
    
    namespace gy_ccg_local.App_Code.WTDModels
    {
        public class WtdMemberProfileModel : USNBaseViewModel
        {
            public WtdMemberProfileModel(IPublishedContent content) 
                : base(content)
            {
    
            }
        }
    }
    

    Profile.cshtml - View

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<gy_ccg_local.App_Code.WTDModels.WtdMemberProfileModel>
    
    
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using USN.USNModels
    @using gy_ccg_local.App_Code.WTDModels
    
    @{
        Layout = "USNMaster.cshtml";
        WtdMemberProfileModel profilePage = (WtdMemberProfileModel)Model.Content;
    }
    

    Stack Trace

    [MissingMethodException: No parameterless constructor defined for this object.]
       System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
       System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +119
       System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
       System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
       System.Activator.CreateInstance(Type type) +11
       System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +197
    
    [MissingMethodException: No parameterless constructor defined for this object. Object type 'gy_ccg_local.App_Code.WTDModels.WtdMemberProfileModel'.]
       System.Web.Mvc.DefaultModelBinder.CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) +233
       System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +531
       System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +330
       System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +331
       System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105
       System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743
       System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
       System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343
       System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
       System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465
       System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
       System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
       System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
       System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
       System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
       System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +369
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
    
  • Julien Kulker 75 posts 427 karma points c-trib
    Jan 17, 2019 @ 12:30
    Julien Kulker
    0

    Well maybe i am wrong here but should you do something like this in your view.

    Profile.cshtml - View

     WtdMemberProfileModel profilePage = new WtdMemberProfileModel(Model.Content);
    

    }

    You try to cast the Model.Content object to a ProfileModel wrong.

  • Alex 4 posts 74 karma points
    Jan 17, 2019 @ 15:01
    Alex
    0

    Thanks Julien but that idea didn't work. Your change produces the same resulting error.

    Update

    I corrected the name of the template for the ActionResult to that of the target Template Alias

    WtdMemberProfileController.cs - Controller

    public ActionResult MembersProfile(WTDModels.WtdMemberProfileModel model). 
    

    At first glance I thought I might have cracked it. When I rebuilt and ran the site the page threw up this error:

    CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'SummaryOfMember' and no extension method 'SummaryOfMember' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    I thought initially that was just because I hadn't included any get{} methods in my custom model (which is wrong thinking.. I think). However if I comment out the line:

    Profile.cshtml - View

     @Html.Raw(profilePage.Content.SummaryOfMember);
    

    I get a new error

    Exception Details: Umbraco.Web.Mvc.ModelBindingException: Cannot bind source type Umbraco.Web.Models.RenderModel to model type gyccglocal.App_Code.WTDModels.WtdMemberProfileModel.

    My question is; Why is it trying to bind RenderModel type when in my model I have set to to return that of WtdMemberProfileModel?

    I noticed that USNBaseViewModel inherits from RenderModel. Is ModelBinding trying to bind this?

  • Julien Kulker 75 posts 427 karma points c-trib
    Jan 17, 2019 @ 16:23
    Julien Kulker
    0

    Well i don't know if that is the problem. But models that i return to the view i do this for my viewmodels

     public class MenuViewModel : RenderModel
    
Please Sign in or register to post replies

Write your reply to:

Draft