Apologies for the most likely very newbie question, but hey I'm a newbie to this CMS world.
I have an external database from which I would like to include some data from into my master template.
Anyone have any suggestions on how to accomplish this within the Umbraco framework? Macros, partial views, surface controller, all of the above, none of the above :)
So a surface controller and @Html.Action in the master template can get me there but the problem i have is that the object used in the partial view seems to go out of scope once the partial view is rendered.
Ideally the master template could call a controller which would get an object from a database and return that object as a model, and then that model's properties could be used throughout the template.
For example, this object's properties are used in the header, in the body and in the footer. I would prefer not to call @html.Action 5 times where each simply returns 1 of the properties with a bit of markup.
public class HomePageController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
LogHelper.Info<HomePageController>("Hello, we have a custom model.");
//this line means continue and perform the normal rendering
return base.Index(new CustomRenderModel());
}
}
public class CustomRenderModel : RenderModel
{
public string MyNewProperty;
public CustomRenderModel()
: base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
{
MyNewProperty = "Umbraco rocks!";
}
}
how to add external data to a master template
Apologies for the most likely very newbie question, but hey I'm a newbie to this CMS world.
I have an external database from which I would like to include some data from into my master template.
Anyone have any suggestions on how to accomplish this within the Umbraco framework? Macros, partial views, surface controller, all of the above, none of the above :)
thanks in advance
Hi,
you need to create a normal connection string into the web.config and so use the .NET libraries to access data and display them.
Hi Greg,
If you want to include some data to all pages, than you have to use master template and partial view maybe.
If you need to insert some data only to one documentType - than you can use RenderMvcController, read more - https://our.umbraco.org/documentation/reference/routing/custom-controllers
SurfaceControllers are for interactions between clien and server, form handling for example.
Thanks,
Alex
thanks for the reply Alex.
So a surface controller and @Html.Action in the master template can get me there but the problem i have is that the object used in the partial view seems to go out of scope once the partial view is rendered.
Ideally the master template could call a controller which would get an object from a database and return that object as a model, and then that model's properties could be used throughout the template.
For example, this object's properties are used in the header, in the body and in the footer. I would prefer not to call @html.Action 5 times where each simply returns 1 of the properties with a bit of markup.
Hi Greg,
I think you have to use Custom Models from, read more here - https://github.com/kgiszewski/LearnUmbraco7/blob/master/Chapter%2006%20-%20Surface,%20WebAPI%20and%20RenderMVC%20Controllers/01%20-%20RenderMVC%20Controllers.md
Example:
Thanks,
Alex
yup this will work, thanks again Alex
is working on a reply...