In your Content Views you will often want to get data either from the current page or get content from the database
The Content property is a default parameter and passing into the page.
Getting Content Properties
In your IContentView's you can get data using the following methods (Property aliases are used, these can be found on the Content Type page under the name of the property you have added)
Get a content property
@(Content!.GetValue<string>("PropertyAlias"))
The GetValue<TypeHere> accepts from types. Each content property has an example how how to use this method to get the data and the type to use. See the link below
To get media or content from a picker , it's a bit different, you need to pass in an instance of Mediatr. If you haven't defined it in the _imports like I have, make sure you Inject and instance of it.
Each Command has similar but sometimes different properties, here is the content command options
public class QueryContentCommand : IRequest<PaginatedList<Models.Content>>
{
public string ContentTypeAlias { get; set; } = string.Empty;
public Guid? ContentTypeId { get; set; }
public bool AsNoTracking { get; set; } = true;
public List<Guid> Ids { get; set; } = [];
public int PageIndex { get; set; } = 1;
public int AmountPerPage { get; set; } = 10;
public string? SearchTerm { get; set; }
public bool IncludeChildren { get; set; }
public GetContentsOrderBy OrderBy { get; set; } = GetContentsOrderBy.DateUpdatedDescending;
public Expression<Func<Models.Content, bool>>? WhereClause { get; set; }
public IQueryable<Models.Content>? Query { get; set; }
}
Query*Command returns a paged list, which allows you to page server side, you just need to page in the PageIndex and AmountPerPage for the paging
public class PaginatedList<T>
{
public int PageIndex { get; set; }
public int TotalPages { get; set; }
public int TotalItems { get; set; }
public IEnumerable<T> Items { get; set; } = Enumerable.Empty<T>();
public PaginatedList()
{
}
public PaginatedList(IQueryable<T> items, int pageIndex, int pageSize)
{
var count = items.Count();
PageIndex = pageIndex-1;
TotalPages = (int) Math.Ceiling(count / (double) pageSize);
var skip = PageIndex * pageSize;
Items = skip > 0 ? items.Skip(skip).Take(pageSize).ToList() : items.Take(pageSize).ToList();
TotalItems = count;
}
public bool HasPreviousPage => PageIndex > 1;
public bool HasNextPage => PageIndex < TotalPages;
}
You can use the same pattern for getting almost any type of data. i.e.
await Mediator.Send(new QueryUsersCommand { Options added here })
await Mediator.Send(new QueryRolesCommand { Options added here })
await Mediator.Send(new QueryMediaCommand { Options added here })
await Mediator.Send(new QueryContentTypesCommand { Options added here })
await Mediator.Send(new QueryAuditsCommand { Options added here })
await Mediator.Send(new QueryLanguageCommand { Options added here })
await Mediator.Send(new QueryDomainCommand { Options added here })
Get Command
The Get*Command pattern returns a single item. For example the code below, returns a single content item by ID and includes all child content items
var page = await Mediator.Send(new GetContentCommand
{
Id = content.Id,
IncludeChildren = true
});
Again, you use the same pattern for all other data
await Mediator.Send(new GetUserCommand { Options added here })
await Mediator.Send(new GetRoleCommand { Options added here })
await Mediator.Send(new GetMediaCommand { Options added here })
await Mediator.Send(new GetContentTypeCommand { Options added here })
await Mediator.Send(new GetDomainCommand { Options added here })
await Mediator.Send(new GetGlobalDataCommand { Options added here })
await Mediator.Send(new GetGlobalLanguageCommand { Options added here })
Save Command
If you want to programmatically save some content or media, we have a similar pattern for that too. They all follow the Save*Command i.e. SaveContentCommand, SaveMediaCommand etc...