Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
This section will describe how you can render content from other nodes besides the current page in your MVC Views
The easiest way to get some content by Id is to use the following syntax (where 1234 is the content id you'd like to query for):
You can also query for multiple content items using multiple ids:
This syntax will support an unlimited number of Ids passed to the method.
You can also retrieve content using the Guid
Id. In the example "ca4249ed-2b23-4337-b522-63cabe5587d1" is the key of the content.
You can also pass a Udi to retrieve the content.
The same query structures apply to media:
All of these extension methods are available on Umbraco.Core.Models.IPublishedContent
so you can have strongly typed access to all of them with intellisense for both content and media. The following methods return IEnumerable<IPublishedContent>
Additionally there are other methods that will return a single IPublishedContent
With the IPublishedContent
model we support strongly typed LINQ queries out of the box so you will have intellisense for that.
The two examples below have not been verified for Umbraco 9 and 10 yet.
therefore they might not work on the latest versions of Umbraco.
With the strongly typed IPublishedContent
you can do complex queries.
How to work with MVC templates in Umbraco.
Working with MVC Views and Razor syntax in Umbraco
Documentation covering how to use Partial Views. This is not documentation about using "Partial View Macros", this documentation relates to using native MVC partial views within Umbraco.
Using MVC Child Actions in Umbraco
How to query for published data in your Views
In the previous versions of MVC, we used Child Actions to build reusable components/widgets consisting of both Razor markup and backend logic. The backend logic was implemented as a controller action and marked with a [ChildActionOnly] attribute. Child Actions are no longer supported in ASP.NET Core MVC. Instead, we will use the View Component feature.
View components replace the traditional Controller (SurfaceController)/Partial View relationship and instead offers a modular approach of separating your views in to several smaller units. View Components are self-contained objects that consistently render HTML from a Razor view.
View components are:
Generated from a C# class
Derived from the base class ViewComponent and
Associated with a Razor file (*.cshtml) to generate markup.
View components are similar to partial views but they are much more powerful compared to the partial views. View components do not use model binding, instead they work with the data provided when calling it.
View Components can be implemented in any part of the web application where there are some possibilities to duplicate code like Header, Navigation Pane, Login Panel, Menu, Shopping Cart, Footer, BlockList Items and so on. View Components behave like a web part containing both business logic and UI design to create a package which can be reused in multiple parts of the web application.
A view component code consists of two parts:
The View Component class derived from the ViewComponent
class:
Returns a Task object as IViewComponentResult
:
In this example, let's create a ViewComponent for a Product List and render it on the HomePage of the website.
Create a folder named ProductView. In this folder, create a new class named ProductViewViewComponent.cs as below:
In Views folder, create new folders at Views\Shared\Components\ProductView
. In the ProductView folder, create a new file named Default.cshtml as below:
Adding the following declaration will give access to the UmbracoHelper object inside the ViewComponent View
You can invoke a ViewComponent from anywhere (even from within a Controller or another ViewComponent). Since this is our Product List, we want it rendered on the Home page - so we’ll invoke it from our HomePage.cshtml file using:
You can read about different ways of invoking your view component in the View components in ASP.NET Core section of the Microsoft Documentation.view=aspnetcore-5.0)
By default, the framework searches for the Component View path in the following areas:
/Views/{Controller Name Folder}/Components/{View Component Name Folder}/{View Name}
/Views/Shared/Components/{View Component Name Folder}/{View Name}
Working with MVC Views and Razor syntax in Umbraco
All Umbraco views inherit from Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.NameOfYourDocType>
along with the using statement @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
. This exposes many properties that are available in razor. The properties on the Document Type can be accessed in a number of ways:
@Model (of type Umbraco.Web.Mvc.ContentModel
) -> the model for the view which contains the standard list of IPublishedContent properties but also gives you access to the typed current page (of type whatever type you have added in the angled brackets).
@Umbraco (of type UmbracoHelper
) -> contains many helpful methods, from rendering macros and fields to retrieving content based on an Id and tons of other helpful methods. See UmbracoHelper Documentation
@Html (of type HtmlHelper
) -> the same HtmlHelper you know and love from Microsoft but we've added a bunch of handy extension methods like @Html.BeginUmbracoForm
@UmbracoContext (of type Umbraco.Cms.Web.Common.UmbracoContext
)
This is probably the most used method which renders the contents of a field using the alias of the content item.
If you're using the method from within a partial view then be aware that you will need to inherit the context so the method knows which type to get the desired value from. You'd do this at the top of partial view and so strongly typed properties can then be accessed in the partial view. For instance you can pass "HomePage" like this:
You will also need to pass the "Context" to the @Model.Value() method if you're looping over a selection like this where we pass the "item" variable.
Looping over a selection works in a similar way. If you have a property that contains, for instance, an IEnumberable collection, you can access the individual items using a foreach loop. Below illustrates how you might do that using "item" as a variable.
If you want to convert a type and it's possible, you can do that by typing a variable and assigning the value from your property to it. This could look like the example below.
In this example, we are looping through a list of items with the custom made type TeamMember assigned. This means we are able to access the strongly typed properties on the TeamMember item.
Rendering a macro is done using UmbracoHelper. There are 3 overloads, we'll start with the most basic:
This renders a macro with the specified alias without any parameters:
This renders a macro with some parameters using an anonymous object:
This renders a macro with some parameters using a dictionary
IMemberManager
is the gateway to everything related to members when templating your site. IMemberManager Documentation
Models Builder allows you to use strongly typed models in your views. Properties created on your document types can be accessed with this syntax:
When Models Builder resolve your properties it will also try to use value converters to convert the values of your data into more convenient models. This allows you to access nested objects as strong types instead of having to rely on dynamics and risking having a lot of potential errors when working with these.
This section will show you how to use MVC Partial Views in Umbraco.
Please note, this is documentation relating to the use of native MVC partial views, not 'Partial View Macros'
Using Partial Views in Umbraco is exactly the same as using Partial Views in a normal MVC project. There is detailed documentation on the Internet about creating and using MVC partial views
Partial views allow you to reuse components between your views (templates).
The locations to store Partial Views when rendering in the Umbraco pipeline is:
The standard MVC partial view locations will also work:
The ~/Views/Render
location is valid because the controller that performs the rendering in the Umbraco codebase is the: Umbraco.Cms.Web.Common.Controllers.RenderController
If however you are Hijacking an Umbraco route and specifying your own controller to do the execution, then your partial view location can also be:
A quick example of a content item that has a template that renders out a partial view template for each of its child documents:
The MVC template markup for the document:
The partial view (located at: ~/Views/Partials/ChildItem.cshtml
)
Normally you would create a partial view by using the @model MyModel
syntax. However, inside of Umbraco you will probably want to have access to the handy properties available on your normal Umbraco views like the Umbraco helper: @Umbraco
and the Umbraco context: @UmbracoContext
. The good news is that this is possible. Instead of using the @model MyModel
syntax, you need to inherit from the correct view class, so do this instead:
By inheriting from this view, you'll have instant access to those handy properties and have your view created with a strongly typed custom model.
Another case you might have is that you want your Partial View to be strongly typed with the same model type (IPublishedContent
) as a normal template if you are passing around instances of IPublishedContent. To do this, have your partial view inherit from Umbraco.Cms.Web.Common.Views.UmbracoViewPage
(like your normal templates). When you render your partial, a neat trick is that you can pass it an instance of IPublishedContent
. For example:
You don't normally need to cache the output of Partial views, like you don't normally need to cache the output of User Controls, but there are times when this is necessary. Like macro caching, we provide caching output of partial views. This is done by using an HtmlHelper extension method:
The above will cache the output of your partial view for one hour when not running Umbraco in debug
mode. Additionally, there are a few optional parameters you can specify to this method. Here is the full method signature:
So you can specify to cache by member and/or by page and also specify additional view data to your partial view. * However*, if your view data is dynamic (meaning it could change per page request) the cached output will still be returned. This same principle applies if the model you are passing in is dynamic. Please be aware of this: if you have a different model or viewData for any page request, the result will be the cached result of the first execution. If this is not desired you can generate your own cache key to differentiate cache instances using the contextualKeyBuilder parameter
To create multiple versions based on one or more viewData parameters you can do something like this:
Or using a custom helper function:
Or even based on a property on the Model (though if Model is the current page then cacheByPage
should be used instead):
Regardless of the complexity here the contextualKeyBuilder function needs to return a single string value.
Caching is only enabled when your application has debug="false"
. When debug="true"
caching is disabled. Also, the cache of all CachedPartials is emptied on Umbraco publish events.
Lots of examples of using various techniques to render data in a view
Creating an HTML form to submit data with MVC in Umbraco is possible in a few steps.
If you want to create a Form:
Using view models, views, controllers, and a handy HtmlHelper extension method called BeginUmbracoForm, see the Creating Forms article.
Using Umbraco Forms, see the Umbraco Forms Documentation article.