Hi Alex I want to inherit my custom Model but retain access to RenderModel properties like @Model.GetPropertyValue("bodyText") etc. The problem for me lies in the model not the view because it suggests that Umbraco.Web.Models.RenderModel doesn not contain a construction that takes 0 arguments.
As you suspected, there are no parameterless constructors.
The easiest way to achieve what (I think) you want, is to override the Index method in the controller, so:
namespace JEC.Web.Models
{
public class Product : Umbraco.Web.Models.RenderModel
{
public override ActionResult Index(RenderModel renderModel)
{
ExampleModel myModel = new ExampeModel();
// Perform your mappings here (or call a mapper)
myModel.PageTitle = renderModel.GetPropertyValue<string>("pageTitle");
// If you really want access to the IPublishedContent in your view, simply add it
// to your strongly typed model
myModel.Content = renderModel;
// Then spit it back to your view
return myModel;
}
}
public class ExampleModel
{
public string PageTitle { get; set; }
public IPublishedContent Content { get; set; }
}
}
Now in your view, you simply add the model type, and query it to build up your page.
@model JEC.Web.Models.ExampleModel
<h1>@Model.PageTitle</h1>
@* You can also query the IPublishedContent too if you wish *@
<p>@Model.Content.GetPropertyValue<string>("welcomeMessage")</p>
It didn't work and tried your solution also but as there is a shared layouts page your method doesn't work either :(
public class test : Umbraco.Web.Models.RenderModel
{
public int ID { get; set; }
}
public override ActionResult Index(test model)
{
model.ID = 1;
return View(model);
}
Now with an error message no suitable method found to override.
If you want to inherit from RenderModel, you could create a constructor that takes an IPublishedContent and calls the base class constructor. Something like this:
public class Product: Umbraco.Web.Models.RenderModel
{
public Product(IPublishedContent content)
: base(content)
{
}
}
That is how I usually do it. Depending on your use case, you may have to instatiate your model manually in your controller:
var model = new Product(CurrentPage);
This seems to work for me, but I am sure there are probably much smarter ways to do this.
Custom Controllers
Hi,
I'm trying to follow the following document on how to create a customer controller.
I'm currently stuck at the bit which says:
Make your custom model inherit from
Umbraco.Web.Models.RenderModel
I assumed it meant something like this?
however it's sugests Ithere is no constructor which takes no arguments i.e. needs to be
or
https://our.umbraco.org/Documentation/Reference/Mvc/custom-controllers
Hi John,
Can you show your view ? Can you inherit your views from UmbracoViewPage?
It solves problem for me.
Thanks, Alex
Hi Alex I want to inherit my custom Model but retain access to RenderModel properties like @Model.GetPropertyValue("bodyText") etc. The problem for me lies in the model not the view because it suggests that Umbraco.Web.Models.RenderModel doesn not contain a construction that takes 0 arguments.
I'm trying to understand this point in the documentation
That should do the trick
Hi John,
As you suspected, there are no parameterless constructors.
The easiest way to achieve what (I think) you want, is to override the Index method in the controller, so:
Now in your view, you simply add the model type, and query it to build up your page.
Hope this helps!
Cheers,
HJR
Well I couldn't work out how to edit my post :/
There is a small correction:
And
And the controller should extend RenderMvcController, not RenderModel - sorry, I think I've totally misunderstood your requirement here.
Thanks Hywel I'll give that a try.
Hi John,
If I could work out how to mark as a solution, I would :)
Glad to have been a help.
Cheers,
Hywel
Hi Hywel,
It didn't work and tried your solution also but as there is a shared layouts page your method doesn't work either :(
Now with an error message no suitable method found to override.
If you want to inherit from RenderModel, you could create a constructor that takes an IPublishedContent and calls the base class constructor. Something like this:
That is how I usually do it. Depending on your use case, you may have to instatiate your model manually in your controller:
This seems to work for me, but I am sure there are probably much smarter ways to do this.
Hi Asbjørn, could you provide a more detailed example with controller?
My attempt....
Model
Controller
View
Sorry for not getting back to you sooner.
Your example looks pretty good, except for the controller. If you are doing route hijacking, you would usually just call CurrentTemplate(), like this:
You can also just call an MVC view in the standard MVC way, if you want to.
is working on a reply...