Creating parameterless constructor for custom ViewModels umbraco 8
Hello Umbraco people,
I tried to post back a form using a custom model to a surface controller and got 'No parameterless constructor defined for this object'. My model is as follows:
public class ExampleViewModel : ContentModel
{
public ExampleViewModel(IPublishedContent content) : base(content){ }
//properties etc...
}
My controller is as follows:
public class ExampleSurfaceController : SurfaceController
{
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ExampleAction(ExampleViewModel model)
{
//do things
}
}
I know from previous versions of umbraco this could be achieved by adding the following to the model:
public ExampleViewModel () : base(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) { }
My question is, how is this achieved in Umbraco 8?
public class ExampleController : RenderMvcController
{
[HttpGet]
public ActionResult ExampleAction(ContentModel contentModel)
{
var model = new ExampleViewModel(contentModel.Content);
return CurrentTemplate(model);
}
}
Creating parameterless constructor for custom ViewModels umbraco 8
Hello Umbraco people,
I tried to post back a form using a custom model to a surface controller and got 'No parameterless constructor defined for this object'. My model is as follows:
My controller is as follows:
I know from previous versions of umbraco this could be achieved by adding the following to the model:
My question is, how is this achieved in Umbraco 8?
Why do you require the submitted model to derive from content model?
I use it when rendering the view:
Found the solution, by using the Umbraco.Web.Composing library and adding this constructor to my model:
As long as you know what dependencies you are relying on :)
Saved my day!!
is working on a reply...