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)
{
}
}
}
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?
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:
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
WtdMemberProfile.cs - Model
Profile.cshtml - View
Stack Trace
Well maybe i am wrong here but should you do something like this in your view.
Profile.cshtml - View
}
You try to cast the Model.Content object to a ProfileModel wrong.
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
At first glance I thought I might have cracked it. When I rebuilt and ran the site the page threw up this error:
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
I get a new error
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?
Well i don't know if that is the problem. But models that i return to the view i do this for my viewmodels
is working on a reply...