Table of Contents

Class ViewDataDictionaryExtensions

Namespace
Cuemon.Extensions.AspNetCore.Mvc
Assembly
Cuemon.Extensions.AspNetCore.Mvc.dll

Extension methods for the ViewDataDictionary class. Experimental.

public static class ViewDataDictionaryExtensions
Inheritance
ViewDataDictionaryExtensions

Examples

The following example follows the same pattern as the sample MVC app in the test project: controller actions populate breadcrumbs from the current model, and a shared Razor partial reads them back from . Three RegionController actions call AddBreadcrumbs with a RegionPageModel that holds hierarchical labels. A BreadcrumbPartial class then retrieves the breadcrumbs via GetBreadcrumbs and projects each into a readable string, demonstrating hierarchical navigation-data propagation in ASP.NET Core MVC.

using System.Collections.Generic;
using System.Linq;
using Cuemon.Extensions.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

namespace Cuemon.Extensions.AspNetCore.Mvc.DocExamples;

public sealed class RegionController : Controller
{
    public IActionResult Index()
    {
        var model = new RegionPageModel("Regions", "Northern Europe", "Danish");
        ViewData.AddBreadcrumbs(this, model, page => page.Labels);
        return View(model);
    }

    public IActionResult Region(string regionName, string regionDisplayName)
    {
        var model = new RegionPageModel("Regions", regionDisplayName, "Danish");
        ViewData.AddBreadcrumbs(this, model, page => page.Labels);
        return View("CultureCollection", model);
    }

    public IActionResult Culture(string regionName, string regionDisplayName, string cultureName)
    {
        var model = new RegionPageModel("Regions", regionDisplayName, cultureName);
        ViewData.AddBreadcrumbs(this, model, page => page.Labels);
        return View("Culture", model);
    }
}

public sealed class BreadcrumbPartial
{
    public IReadOnlyList<string> Render(ViewDataDictionary viewData, IRazorPage currentPage)
    {
        return viewData
            .GetBreadcrumbs(currentPage)
            .Select(link => $"{link.Label} ({link.ControllerName}/{link.ActionName})")
            .ToList();
    }
}

public sealed class RegionPageModel
{
    public RegionPageModel(params string[] labels)
    {
        Labels = labels;
    }

    public IReadOnlyList<string> Labels { get; }
}

Methods

AddBreadcrumbs<T>(ViewDataDictionary, Controller, T, Func<T, IEnumerable<string>>)

Adds a sequence of Breadcrumb objects to the specified viewData.

public static void AddBreadcrumbs<T>(this ViewDataDictionary viewData, Controller controller, T model, Func<T, IEnumerable<string>> initializer)

Parameters

viewData ViewDataDictionary

The ViewDataDictionary to extend.

controller Controller

The controller to resolve all public methods with IActionResult as return type from.

model T

The model to retrieve custom breadcrumb labels from.

initializer Func<T, IEnumerable<string>>

The function delegate that will initialize labels from the spcified model.

Type Parameters

T

The type of the model to retrieve breadcrumb labels from.

GetBreadcrumbs(ViewDataDictionary, IRazorPage)

Gets a sequence of Breadcrumb objects from the specified viewData.

public static IEnumerable<Breadcrumb> GetBreadcrumbs(this ViewDataDictionary viewData, IRazorPage razor)

Parameters

viewData ViewDataDictionary

The ViewDataDictionary to extend.

razor IRazorPage

The razor page from where the breadcrumbs will be rendered.

Returns

IEnumerable<Breadcrumb>

An IEnumerable<T> sequence of Breadcrumb objects (if any).