From 37308fb473644516bb132b92e52a33835412914f Mon Sep 17 00:00:00 2001 From: akdalin Date: Wed, 5 Mar 2025 09:42:39 +0000 Subject: [PATCH 1/5] Create TaskList View, ViewComponent and Model --- .../ViewComponents/TaskListViewComponent.cs | 17 +++++ .../ViewModels/TaskListItemViewModel.cs | 44 +++++++++++++ .../ViewModels/TaskListViewModel.cs | 13 ++++ .../Shared/Components/TaskList/Default.cshtml | 63 +++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 DotnetViewComponents/ViewComponents/TaskListViewComponent.cs create mode 100644 DotnetViewComponents/ViewModels/TaskListItemViewModel.cs create mode 100644 DotnetViewComponents/ViewModels/TaskListViewModel.cs create mode 100644 DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml diff --git a/DotnetViewComponents/ViewComponents/TaskListViewComponent.cs b/DotnetViewComponents/ViewComponents/TaskListViewComponent.cs new file mode 100644 index 0000000000..6f4f3a7811 --- /dev/null +++ b/DotnetViewComponents/ViewComponents/TaskListViewComponent.cs @@ -0,0 +1,17 @@ +namespace DotnetViewComponents.ViewComponents +{ + using System.Collections.Generic; + using Microsoft.AspNetCore.Mvc; + using DotnetViewComponents.ViewModels; + + public class TaskListViewComponent : ViewComponent + { + public IViewComponentResult Invoke( + IEnumerable listItems) + { + var model = new TaskListViewModel(listItems); + + return View(model); + } + } +} diff --git a/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs b/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs new file mode 100644 index 0000000000..23cbc0e99d --- /dev/null +++ b/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs @@ -0,0 +1,44 @@ +namespace DotnetViewComponents.ViewModels +{ + public class TaskListItemViewModel + { + /// + /// Creates a new instance of the class. + /// STATE: Choose between "Completed", "Incomplete" and "Cannot start yet". + /// STATESTYLE: The style should match the state. (e.g. "TaskStateStyle.INCOMPLETE" for "Incomplete", "TaskStateStyle.COMPLETED" for "Completed", "TaskStateStyle.UNSTARTED" for "Cannot start yet") + /// + /// Choose between "Completed", "Incomplete" and "Cannot start yet". + /// The style should match the state. (e.g. "TaskStateStyle.INCOMPLETE" for "Incomplete", "TaskStateStyle.COMPLETED" for "Completed", "TaskStateStyle.UNSTARTED" for "Cannot start yet") + public TaskListItemViewModel( + string id, + string name, + string state, + TaskStateStyle stateStyle, + string hintText) + { + Id = id; + Name = name; + State = state; + StateStyle = stateStyle; + HintText = hintText; + } + + public string Id { get; set; } + + public string Name { get; set; } + + public string State { get; set; } + + public TaskStateStyle StateStyle { get; set; } + + public string HintText { get; set; } + } + + + public enum TaskStateStyle + { + COMPLETED, + INCOMPLETE, + UNSTARTED + } +} diff --git a/DotnetViewComponents/ViewModels/TaskListViewModel.cs b/DotnetViewComponents/ViewModels/TaskListViewModel.cs new file mode 100644 index 0000000000..3cc6d02e00 --- /dev/null +++ b/DotnetViewComponents/ViewModels/TaskListViewModel.cs @@ -0,0 +1,13 @@ +namespace DotnetViewComponents.ViewModels +{ + public class TaskListViewModel + { + public TaskListViewModel( + IEnumerable listItems) + { + ListItems = listItems; + } + + public IEnumerable ListItems { get; set; } + } +} diff --git a/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml b/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml new file mode 100644 index 0000000000..d4aaf3a8fd --- /dev/null +++ b/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml @@ -0,0 +1,63 @@ +@using DotnetViewComponents.ViewModels +@model TaskListViewModel + +
    + @foreach (var task in Model.ListItems) + { + + @if (task.StateStyle == TaskStateStyle.COMPLETED) + { + + } + + else if (task.StateStyle == TaskStateStyle.INCOMPLETE) + { + + } + + else if (task.StateStyle == TaskStateStyle.UNSTARTED) + { +
  • +
    +
    @task.Name
    + @if (!String.IsNullOrWhiteSpace(task.HintText)) + { +
    @task.HintText
    + } +
    +
    + @task.State +
    +
  • + } + + } +
From ffcdf6dd351a07647e7dc3bafcb50fa5f114128d Mon Sep 17 00:00:00 2001 From: akdalin Date: Wed, 5 Mar 2025 09:48:27 +0000 Subject: [PATCH 2/5] Parametrise URL --- DotnetViewComponents/ViewModels/TaskListItemViewModel.cs | 4 ++++ .../Views/Shared/Components/TaskList/Default.cshtml | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs b/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs index 23cbc0e99d..c10de92473 100644 --- a/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs +++ b/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs @@ -12,12 +12,14 @@ public class TaskListItemViewModel public TaskListItemViewModel( string id, string name, + string url, string state, TaskStateStyle stateStyle, string hintText) { Id = id; Name = name; + url = url; State = state; StateStyle = stateStyle; HintText = hintText; @@ -27,6 +29,8 @@ public TaskListItemViewModel( public string Name { get; set; } + public string Url { get; set; } + public string State { get; set; } public TaskStateStyle StateStyle { get; set; } diff --git a/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml b/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml index d4aaf3a8fd..16da4a9529 100644 --- a/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml +++ b/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml @@ -9,7 +9,7 @@ { } From 15db0b8dc3de971ce220a94f802a297b8feef333 Mon Sep 17 00:00:00 2001 From: akdalin Date: Thu, 1 May 2025 12:50:17 +0100 Subject: [PATCH 5/5] Replace URL with asp dynamically generated link path . Document code --- .../ViewModels/TaskListItemViewModel.cs | 44 +++++++++++++++++-- .../ViewModels/TaskListViewModel.cs | 7 +++ .../Shared/Components/TaskList/Default.cshtml | 2 +- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs b/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs index 2d55b83ebc..b9ad1877db 100644 --- a/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs +++ b/DotnetViewComponents/ViewModels/TaskListItemViewModel.cs @@ -7,34 +7,72 @@ public class TaskListItemViewModel /// STATE: Choose between "Completed", "Incomplete" and "Cannot start yet". /// STATESTYLE: The style should match the state. (e.g. "TaskStateStyle.INCOMPLETE" for "Incomplete", "TaskStateStyle.COMPLETED" for "Completed", "TaskStateStyle.UNSTARTED" for "Cannot start yet") /// + /// The unique identifier for the task. + /// The name of the task. + /// The asp-controller associated with the task's action. + /// The asp-action action associated with the task's action. + /// An asp-all-data-route dictionary containing route data for the task's action. /// Choose between "Completed", "Incomplete" and "Cannot start yet". /// The style should match the state. (e.g. "TaskStateStyle.INCOMPLETE" for "Incomplete", "TaskStateStyle.COMPLETED" for "Completed", "TaskStateStyle.UNSTARTED" for "Cannot start yet") + /// Additional text providing hints about the task. public TaskListItemViewModel( string id, string name, - string url, + string aspController, + string aspAction, + Dictionary aspRouteData, string state, TaskStateStyle stateStyle, string hintText) { Id = id; Name = name; - Url = url; + AspController = aspController; + AspAction = aspAction; + AspRouteData = aspRouteData; State = state; StateStyle = stateStyle; HintText = hintText; } + /// + /// Gets or sets the unique identifier for the task. + /// public string Id { get; set; } + /// + /// Gets or sets the name of the task. + /// public string Name { get; set; } - public string Url { get; set; } + /// + /// Gets or sets the asp-controller of the link. + /// + public string AspController { get; set; } + + /// + /// Gets or sets the as-action of the link. + /// + public string AspAction { get; set; } + + /// + /// Gets or sets the asp-all-route-data of the link. + /// + public Dictionary AspRouteData { get; set; } + /// + /// Gets or sets the current state of the task. + /// public string State { get; set; } + /// + /// Gets or sets the style that matches the state of the task. + /// public TaskStateStyle StateStyle { get; set; } + /// + /// Gets or sets additional text providing hints about the task. + /// public string HintText { get; set; } } diff --git a/DotnetViewComponents/ViewModels/TaskListViewModel.cs b/DotnetViewComponents/ViewModels/TaskListViewModel.cs index 3cc6d02e00..1de7a2e5bb 100644 --- a/DotnetViewComponents/ViewModels/TaskListViewModel.cs +++ b/DotnetViewComponents/ViewModels/TaskListViewModel.cs @@ -2,12 +2,19 @@ namespace DotnetViewComponents.ViewModels { public class TaskListViewModel { + /// + /// Initializes a new instance of the class. + /// + /// A collection of task list items. public TaskListViewModel( IEnumerable listItems) { ListItems = listItems; } + /// + /// Gets or sets the collection of task list items. + /// public IEnumerable ListItems { get; set; } } } diff --git a/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml b/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml index 93c4d27422..0a33cf0359 100644 --- a/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml +++ b/DotnetViewComponents/Views/Shared/Components/TaskList/Default.cshtml @@ -21,7 +21,7 @@
@if (hasLink) { - + @task.Name }