From c16a31c3da1b6bcde11e5d01f2a842a443e048b3 Mon Sep 17 00:00:00 2001 From: akdalin Date: Tue, 29 Apr 2025 15:49:29 +0100 Subject: [PATCH 1/2] Create Hero View, ViewComponent and Model --- .../ViewComponents/HeroViewComponent.cs | 18 ++++++++++ .../ViewModels/HeroViewModel.cs | 23 +++++++++++++ .../Shared/Components/Hero/Default.cshtml | 33 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 DotnetViewComponents/ViewComponents/HeroViewComponent.cs create mode 100644 DotnetViewComponents/ViewModels/HeroViewModel.cs create mode 100644 DotnetViewComponents/Views/Shared/Components/Hero/Default.cshtml diff --git a/DotnetViewComponents/ViewComponents/HeroViewComponent.cs b/DotnetViewComponents/ViewComponents/HeroViewComponent.cs new file mode 100644 index 0000000000..dc614009a7 --- /dev/null +++ b/DotnetViewComponents/ViewComponents/HeroViewComponent.cs @@ -0,0 +1,18 @@ +namespace DotnetViewComponents.ViewComponents +{ + using Microsoft.AspNetCore.Mvc; + using DotnetViewComponents.ViewModels; + + public class HeroViewComponent : ViewComponent + { + public IViewComponentResult Invoke( + string imageSrc = null, + string heading = null, + string textContent = null) + { + var model = new HeroViewModel(imageSrc, heading, textContent); + + return View(model); + } + } +} diff --git a/DotnetViewComponents/ViewModels/HeroViewModel.cs b/DotnetViewComponents/ViewModels/HeroViewModel.cs new file mode 100644 index 0000000000..3eeb7a4334 --- /dev/null +++ b/DotnetViewComponents/ViewModels/HeroViewModel.cs @@ -0,0 +1,23 @@ +namespace DotnetViewComponents.ViewModels +{ + public class HeroViewModel + { + public HeroViewModel( + string? imageSrc, + string? heading, + string? textContent) + { + ImageSrc = imageSrc; + Heading = heading; + TextContent = textContent; + } + + public string? ImageSrc { get; set; } + + + public string? Heading { get; set;} + + + public string? TextContent { get; set;} + } +} diff --git a/DotnetViewComponents/Views/Shared/Components/Hero/Default.cshtml b/DotnetViewComponents/Views/Shared/Components/Hero/Default.cshtml new file mode 100644 index 0000000000..1192841720 --- /dev/null +++ b/DotnetViewComponents/Views/Shared/Components/Hero/Default.cshtml @@ -0,0 +1,33 @@ +@using DotnetViewComponents.ViewModels +@model HeroViewModel + +@{ + var hasImage = !String.IsNullOrWhiteSpace(Model.ImageSrc); + var hasHeading = !String.IsNullOrWhiteSpace(Model.Heading); + var hasTextContent = !String.IsNullOrWhiteSpace(Model.TextContent); +} +
+
+ @if (hasHeading || hasTextContent) + { +
+
+
+
+ @if (hasHeading) + { +

@Model.Heading

+ } + @if (hasTextContent) + { +

@Model.TextContent

+ } + + +
+
+
+
+ } +
+
From 8904ae7769ca24395819fff892469778995c8966 Mon Sep 17 00:00:00 2001 From: akdalin Date: Thu, 29 May 2025 08:53:38 +0100 Subject: [PATCH 2/2] Make view component variables nullable. Add XML comment. --- .../ViewComponents/HeroViewComponent.cs | 9 ++++++--- .../ViewModels/HeroViewModel.cs | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/DotnetViewComponents/ViewComponents/HeroViewComponent.cs b/DotnetViewComponents/ViewComponents/HeroViewComponent.cs index dc614009a7..cfd47adcd8 100644 --- a/DotnetViewComponents/ViewComponents/HeroViewComponent.cs +++ b/DotnetViewComponents/ViewComponents/HeroViewComponent.cs @@ -3,12 +3,15 @@ namespace DotnetViewComponents.ViewComponents using Microsoft.AspNetCore.Mvc; using DotnetViewComponents.ViewModels; + /// + /// A ViewComponent that rendes a hero section. + /// public class HeroViewComponent : ViewComponent { public IViewComponentResult Invoke( - string imageSrc = null, - string heading = null, - string textContent = null) + string? imageSrc = null, + string? heading = null, + string? textContent = null) { var model = new HeroViewModel(imageSrc, heading, textContent); diff --git a/DotnetViewComponents/ViewModels/HeroViewModel.cs b/DotnetViewComponents/ViewModels/HeroViewModel.cs index 3eeb7a4334..e1a79313c5 100644 --- a/DotnetViewComponents/ViewModels/HeroViewModel.cs +++ b/DotnetViewComponents/ViewModels/HeroViewModel.cs @@ -2,6 +2,12 @@ namespace DotnetViewComponents.ViewModels { public class HeroViewModel { + /// + /// Initializes a new instance of the class. + /// + /// The source URL of the hero image. + /// The heading text for the hero section. + /// The main text content for the hero section. public HeroViewModel( string? imageSrc, string? heading, @@ -12,12 +18,19 @@ public HeroViewModel( TextContent = textContent; } + /// + /// Gets or sets the source URL of the hero image. + /// public string? ImageSrc { get; set; } - + /// + /// Gets or sets the heading text for the hero section. + /// public string? Heading { get; set;} - + /// + /// Gets or sets the main text content for the hero section. + /// public string? TextContent { get; set;} } }