Sebastian Dammark
09/20/2024, 6:45 AMskttl
09/20/2024, 7:19 AMdiff
data-row-span="@item.RowSpan"
style=" --umb-block-grid--item-column-span: @item.ColumnSpan; --umb-block-grid--item-row-span: @item.RowSpan; ">
@{
- var partialViewName = "blockgrid/Components/" + item.Content.ContentType.Alias;
+ var partialViewName = "blocks/Components/" + item.Content.ContentType.Alias;
try
{
- @await Html.PartialAsync(partialViewName, item)
+ @await Html.PartialAsync(partialViewName, item.Content, new ViewDataDictionary(ViewData){ { "settings", item.Settings }});
}
catch (InvalidOperationException)
{
And then the partial view is like this (note it now inherits just the content model, and then settings (if needed) is fetched from the viewdata dictionary.
cshtml
@inherits UmbracoViewPage<ContentModels.Accordion>
@{
var settings = ViewData["settings"] as ContentModels.AccordionSetting;
}
Dean Leigh
09/21/2024, 9:03 AMDean Leigh
09/21/2024, 9:05 AMpublic class CustomViewLocation : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ViewEngines.Engines.OfType<RazorViewEngine>().First().ViewLocationFormats = new[]
{
"~/Views/CustomFolder/{0}.cshtml",
"~/Views/Partials/BlockList/Components/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
}
}
bielu
09/21/2024, 11:40 AMskttl
09/21/2024, 5:08 PMbielu
09/21/2024, 9:32 PMDean Leigh
09/22/2024, 8:40 AMbielu
09/22/2024, 8:44 AMDean Leigh
09/22/2024, 9:00 AMbielu
09/22/2024, 9:02 AMMike Chambers
09/22/2024, 10:51 AM<vc:commonRender model="item.Content" settings="item.Settings" />
Ps not sure there is inherently anything wrong with using DataView.. as MVC rehydrates it's models from viewData, which is what you are doing any way when you var settings = ViewData["settings"] as ContentModels.AccordionSetting;
?
Also you do have to watch out for.. @await Html.PartialAsync(partialViewName, item.Content, new ViewDataDictionary(ViewData){ { "settings", item.Settings }});
As you are generating a viewdDataDictionary
from the current ViewData
you might already have Settings
key added.
And isn't <partial name="{name}" model="item.Content" viewData="{vdd}" />
nicer?
(though 6 of one half dozen the other....) I know my frontend devs seem more comfortable with tag helpers.. and in conjunction with the umbraco tag helpers..
<partial umb-if="...." .../>
seems to cause less issues than the @{if (...){ @await Html.PartialAsync(...) }
bielu
09/22/2024, 1:12 PMMike Chambers
09/22/2024, 1:17 PMMike Chambers
09/22/2024, 1:28 PMViewData.ModelState
where rehydration of the page model wasn't occurring, and I had to manually interogate the ViewData.ModelState.TryGetValue("CallToDiscuss", out var callToDiscuss);
where the info was correct.. but that Model.CallToDiscuss
was always posted back as null
Mike Chambers
09/22/2024, 1:37 PMbut you can also pass a new empty
var vdd = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { { "noContainer", true } }; but then you might have model rehydration issues from from modelstate that you have wiped out
Sebastian Dammark
09/23/2024, 8:11 AM@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<IBlockReference>
@using Umbraco.Cms.Web.Common.PublishedModels
@{
var gridModel = Model as BlockGridItem;
var listModel = gridModel == null ? Model as BlockListItem : null;
var block = gridModel?.Content as TextBlock ?? listModel?.Content as TextBlock;
var settings = gridModel?.Settings as TextBlockSettings ?? listModel?.Settings as TextBlockSettings;
if(block != null){
<div class="text-block">
<h1>@block.Title</h1>
<div>@block.Text</div>
</div>
}
}
This way I can just call the partial like this.
<partial name="Blocks/TextBlock" model="block" />
ProBot β¨
09/23/2024, 8:11 AMMike Chambers
09/23/2024, 8:31 AMcsharp
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<IBlockReference>
@using Umbraco.Cms.Web.Common.PublishedModels
@{
var block = Model switch
{
BlockGridItem gridItem => gridItem.Content as TextBlock,
BlockListItem listItem => listItem.Content as TextBlock,
_ => null
};
var settings = Model switch
{
BlockGridItem gridItem => gridItem.Settings as TextBlockSettings,
BlockListItem listItem => listItem.Settings as TextBlockSettings,
_ => null
};
if (block is not null)
{
<div class="text-block">
<h1>@block.Title</h1>
<div>@block.Text</div>
</div>
}
}
Sebastian Dammark
09/23/2024, 8:49 AM