What is the correct way to combine content from Umbraco with external data?
What is the correct way to display list of entities and details from custom database table?
I need to load content for the products list page from Umbraco and products list from external database. For this page I need url like: /products. The page template should display IPublishedContent for this type of pages and paged list of products.
For the product page I need to load content from the Umbraco and product from external database. For this page I need url like: /product/{id}. The page template should display IPublishedContent for this type of pages and details about product by id.
I need to combine content from Umbraco with external data and use custom routes.
Call a service/repo from that controller which connects to the database through ApplicationContext.Current.DatabaseContext.Database;, you can use methods such as Fetch
Create a view model for this data. It needs to inherit from
RenderModel so that IPublishedContent can be used on your view. Do
something along the lines of the following
ProductsViewModel.cs
public class ProductsViewModel : RenderModel
{
public ProductsViewModel(IPublishedContent content) : base(content)
{
}
public List<ProductViewModel> Products { get; set; }
}
Then you should be able to return your ActionResult in your controller with the view model attached.
Then in your view you'll be able to access product data from your custom view model and Model.Content for the IPublishedContent data.
Hope this all makes sense as it's very summarised and you'll have to take a look at those links.
Also, you'll need to repeat all steps for the singular Product page, since it'll have a custom url/route.
public class MasterSurfaceController : SurfaceController
{
[ChildActionOnly]
public ActionResult RenderHeader()
{
ViewBag.HomePage = CurrentPage.AncestorOrSelf(1);
//ViewBag.HomePage = Umbraco.AssignedContentItem.AncestorOrSelf(1);
RenderModel model = new RenderModel(this.CurrentPage);
//RenderModel model = new RenderModel(Umbraco.AssignedContentItem);
return PartialView("~/Views/Partials/Master/Header.cshtml", model);
}
[ChildActionOnly]
public ActionResult RenderFooter()
{
return PartialView("~/Views/Partials/Master/Footer.cshtml");
}
}
For Umbraco pages works okay, for custom product route CurrentPage is unavailable, message:
Cannot find the Umbraco route definition in the route values, the
request must be made in the context of an Umbraco request
How to make CurrentPage available?
Umbraco.AssignedContentItem is available, but I would like to find solution for CurrentPage.
I was asking about CurrentPage in SurfaceController, but for now I only see the way to use Umbraco.AssignedContentItem. The CurrentPage is of type IPublishedContent in SurfaceController.
In the views everything as you described. I'm looking for the way to get CurrentPage in SurfaceController, but most of all this is not possible with custom route.
Umbraco and External data
What is the correct way to combine content from Umbraco with external data?
What is the correct way to display list of entities and details from custom database table?
I need to load content for the products list page from Umbraco and products list from external database. For this page I need url like: /products. The page template should display IPublishedContent for this type of pages and paged list of products.
For the product page I need to load content from the Umbraco and product from external database. For this page I need url like: /product/{id}. The page template should display IPublishedContent for this type of pages and details about product by id.
I need to combine content from Umbraco with external data and use custom routes.
This is quite a big question but I'll try summarise it down into steps I'd do.
ProductsViewModel.cs
Then you should be able to return your ActionResult in your controller with the view model attached.
Then in your view you'll be able to access product data from your custom view model and Model.Content for the IPublishedContent data.
Hope this all makes sense as it's very summarised and you'll have to take a look at those links.
Also, you'll need to repeat all steps for the singular Product page, since it'll have a custom url/route.
Thank you for description.
I developed code according to your suggestions, but have one question to you. I will explain using code.
I use custom route for url {lang}/product/{id}:
The ProductRouteHandler finds dummy product node in Umbraco:
ProductController:
Product.cshtml:
MasterSurfaceController:
For Umbraco pages works okay, for custom product route CurrentPage is unavailable, message:
How to make CurrentPage available?
Umbraco.AssignedContentItem is available, but I would like to find solution for CurrentPage.
@Model.Content will provide you with a strongly typed “CurrentPage”.
CurrentPage is dynamic, whereas Model.Content is an IPublishedContent.
IPublishedContent has methods such as .GetPropertyValue() if that’s what you need it for
I was asking about CurrentPage in SurfaceController, but for now I only see the way to use Umbraco.AssignedContentItem. The CurrentPage is of type IPublishedContent in SurfaceController.
In the views everything as you described. I'm looking for the way to get CurrentPage in SurfaceController, but most of all this is not possible with custom route.
Hmm I can’t help with that I’m sorry.
Why do you need the master controller? I see you’re using the viewbag and returning a model but there may be a way around this pattern
is working on a reply...