Zbu ModelBuilder and BaseController issues - please help!
Hello,
I am using zbuModelBuilder to generate my Models. I am trying to use a BaseController that handles all the common elements (e.g. Page title, pageName etc).
In my baseController I have a function called createViewModel that creates the generic T style model below is that function
protected T CreateViewModel<T>(RenderModel model) where T : new(){
var viewModel = (T)Activator.CreateInstance(typeof(T), model);
return viewModel;
}
Now in my HomeController that extends from BaseController I am trying to call this function like so
public override ActionResult Index(RenderModel model)
{ var viewModel = CreateViewModel<Home>(model);
return CurrentTemplate(viewModel);
}
but keep getting the following error
must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method
Zbu ModelBuilder and BaseController issues - please help!
Hello,
I am using zbuModelBuilder to generate my Models. I am trying to use a BaseController that handles all the common elements (e.g. Page title, pageName etc).
In my baseController I have a function called createViewModel that creates the generic T style model below is that function
protected T CreateViewModel<T>(RenderModel model) where T : new(){
var viewModel = (T)Activator.CreateInstance(typeof(T), model);
return viewModel;
}
Now in my HomeController that extends from BaseController I am trying to call this function like so
public override ActionResult Index(RenderModel model)
{ var viewModel = CreateViewModel<Home>(model);
return CurrentTemplate(viewModel);
}
but keep getting the following error
must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method
Hello,
Have a look at how it's done in the Hybrid Framework: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/BLL/ModelLogic.cs#L25
I also explain how it works in this video: https://www.youtube.com/watch?v=Enni9r0whCE
Jeroen
Thank you very much I am lookig into this now - Looks like what I need!
Hello,
Did you succeed in getting this to work?
Jeroen
is working on a reply...