jacksorjacksor (Richard Jackson)
07/16/2024, 8:55 AMTrackCardViewComponent.cs
) which returns the code for the card, using input of @model Track
. I've included the code at the end of this message.
This works just fine - I more or less want to confirm this is the correct approach, or if I'm over-complicating things. My crucial reason for using the View Component was to pass the Track instance as an parameter to the card, but that might be possible via other means.
All comments/advice/direction appreciated!
Rich
Rootpage.cshtml:
<div class="row row-cols-3 p-0 m-0">
@foreach (Track track in Model.Singles)
{
@await Component.InvokeAsync("TrackCard", track)
}
</div>
TrackCardViewComponent.cs:
public class TrackCardViewComponent : ViewComponent
{
public IViewComponentResult Invoke(Track track)
{
return View("~/Views/ViewComponents/TrackCard.cshtml", track);
}
}
TrackCard.cshtml:
@model Track
@if (Model.TrackArtwork is null) return;
<div class="col-sm-6 col-md-4 p-2">
<img class="w-100" src="@Model.TrackArtwork.MediaUrl()" />
<p>@Model.Name | @Model.TrackArtwork.MediaUrl()</p>
<p>Test</p>
</div>
Barry Fogarty
07/16/2024, 9:09 AMModel.Singles
in the TrackCard partial? That should just have the code block inside the foreach. Otherwise the approach seems fine, my rule of thumb is I use a ViewComponent where I have some custom logic or need a custom ViewModel sent to the partial for whatever reason. If it's just Umbraco content/ModelsBuilder models (e.g. Track
is a native content/element type already) there is no need to add the complexity of a ViewComponent and I'll just do it in the razor code directly.Nik
07/16/2024, 9:26 AMJemayn
07/16/2024, 9:26 AMcsharp
@Html.PartialAsync("~/Views/ViewComponents/TrackCard.cshtml", track)
Jason
07/16/2024, 9:44 AMjacksorjacksor (Richard Jackson)
07/16/2024, 11:03 AMTrackCard.cshtml
partial is being called from Rootpage.cshtml
foreach track in Model.Singles.jacksorjacksor (Richard Jackson)
07/16/2024, 11:05 AMjacksorjacksor (Richard Jackson)
07/16/2024, 11:11 AMJemayn
07/16/2024, 12:39 PMMike Chambers
07/16/2024, 12:57 PMvar vdd = new ViewDataDictionary(ViewData){{"uid", "[GUID]"}}
(but watch for key collisions)
but you can also pass a new empty
var vdd = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { { "noContainer", true } };
(but watch for model rehydration from modelstate)
Might also be nice to put the views in /views/partials/components/
can now see in the backoffice and also viewcomponents will natively look for views in that folder without the absolute view path.
https://www.reddit.com/r/dotnet/comments/1960qyx/a_guide_to_different_ways_of_passing_data_to/Jason
07/16/2024, 1:34 PMMike Chambers
07/16/2024, 1:41 PMMike Chambers
07/16/2024, 1:41 PMMike Chambers
07/16/2024, 1:43 PMJason
07/16/2024, 1:44 PMJason
07/16/2024, 1:45 PMJason
07/16/2024, 1:45 PMMike Chambers
07/16/2024, 1:49 PM@foreach(track){big chunk of html)
in the template. sorry..Jason
07/16/2024, 1:50 PMJason
07/16/2024, 1:51 PM@foreach(track){small chunk of html}
Mike Chambers
07/16/2024, 1:51 PMMike Chambers
07/16/2024, 1:58 PMreturn CurrentUmbracoPage();
the inputs get rehydrated but not the model... not sure if that's me misunderstanding net core.. or umbraco + partial + viewcomponent...
(rehydration seems to happen via the modelstate.. to put input values back but the model remains as if new model())Jason
07/16/2024, 2:00 PM