Umbraco 7.6 Custom Model , and Umbraco Content properties.
Hello community , I am trying to find a possiblity of using following scenario.
I've created a document Type in Umbraco with few properties.
Then i need to fill Custom model with properties from DataBase , so I am doing floowing.
@Html.Action("Index","Dashboard")
The Dashboard Controller is :
public class DashboardController : RenderMvcController
{
public DashboardController()
{
}
// GET: Dashboard
public ActionResult Index()
{
IDashboardService service = new DashboardService();
var model = new DashboardModel(); // ERROR HERE
model.ProjectCount = service.GetTotalProjects();
return PartialView(model);
}
}
my DashboardModel is inherited from RenderModel
public class DashboardModel: RenderModel
{
public DashboardModel(IPublishedContent content): base(content)
{
}
public int ProjectCount { get; set; }
}
But I cannot create an instance of Dashboard Model , since it requireds IPublishedContent in constructor.
What I am trying to achieve is to be able render in Razor both umbraco properties from DocumentType and My Custom Model properties.
Anyone can please point to the right way of how to achieve this ?
So if your document type alias is called 'Dashboard' (and this is all wired up by convention so the naming is important!)
and if you create a controller in your project called DashboardController that inherits RenderMvcController, as you have done, and override the Index action of the RenderMvcController that accepts a RenderModel as a constructor like so....
public class DashboardController : Umbraco.Web.Mvc.RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
//model represents the properties from Umbraco for this content item
// create your model that inherits RenderModel
var mySuperDashboardModel = new DashboardModel(model.Content);
//go off and get the database dashboard stuff
IDashboardService service = new DashboardService();
mySuperDashboardModel.ProjectCount = service.GetTotalProjects();
//pass your enriched model to the current template
return CurrentTemplate(mySuperDashboardModel);
}
}
Your DashboardModel inheriting from RenderModel is correctly defined, passing the IPublishedContent to RenderModel's base constructor.
Now in your view/template you need to adjust the @inherits statement at the top
Now you should be able to access your extra properties
@Model.ProjectCount
but all your existing Umbraco content properties are also available.
So how does the controller fire, in your posted code you were writing Html.Action("index","dashboard"), you don't need to do this, if a page is requested in Umbraco, and it's documenttype alias is called dashboard, then the controller will automatically hijack the request, and your custom code and enrichment of your model will run...
Umbraco 7.6 Custom Model , and Umbraco Content properties.
Hello community , I am trying to find a possiblity of using following scenario.
I've created a document Type in Umbraco with few properties.
Then i need to fill Custom model with properties from DataBase , so I am doing floowing.
The Dashboard Controller is :
my DashboardModel is inherited from RenderModel
But I cannot create an instance of Dashboard Model , since it requireds IPublishedContent in constructor.
What I am trying to achieve is to be able render in Razor both umbraco properties from DocumentType and My Custom Model properties.
Anyone can please point to the right way of how to achieve this ?
Hi Delgado
I'm a little confused on what you are trying to achieve (but think it's just the word dashboard throwing me off the scent)
Essentially what you are describing is very close to a technique called route hijacking
https://our.umbraco.org/documentation/reference/routing/custom-controllers
So if your document type alias is called 'Dashboard' (and this is all wired up by convention so the naming is important!)
and if you create a controller in your project called DashboardController that inherits RenderMvcController, as you have done, and override the Index action of the RenderMvcController that accepts a RenderModel as a constructor like so....
IDashboardService service = new DashboardService(); mySuperDashboardModel.ProjectCount = service.GetTotalProjects(); //pass your enriched model to the current template return CurrentTemplate(mySuperDashboardModel); }
}
Your DashboardModel inheriting from RenderModel is correctly defined, passing the IPublishedContent to RenderModel's base constructor.
Now in your view/template you need to adjust the @inherits statement at the top
Now you should be able to access your extra properties
@Model.ProjectCount
but all your existing Umbraco content properties are also available.
So how does the controller fire, in your posted code you were writing Html.Action("index","dashboard"), you don't need to do this, if a page is requested in Umbraco, and it's documenttype alias is called dashboard, then the controller will automatically hijack the request, and your custom code and enrichment of your model will run...
Marc Thank you very much for such a good answer !!!!
is working on a reply...