Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit ad7a402

Browse files
committed
fix rendering through viewcomponent
1 parent a87e298 commit ad7a402

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

src/Our.Umbraco.DocTypeGridEditor.sln

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ VisualStudioVersion = 16.0.30711.63
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Our.Umbraco.DocTypeGridEditor", "Our.Umbraco.DocTypeGridEditor\Our.Umbraco.DocTypeGridEditor.csproj", "{D4318AFD-033C-46FC-9DD2-9FC57934CD2F}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Our.Umbraco.DocTypeGridEditor.Testsite", "Our.Umbraco.DocTypeGridEditor.Testsite\Our.Umbraco.DocTypeGridEditor.Testsite.csproj", "{D510AF65-1DCB-4A60-99DE-DA1D3B6E1AC7}"
9-
EndProject
108
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4933D56D-BD6F-4C2D-A9CF-40B90148E933}"
119
ProjectSection(SolutionItems) = preProject
1210
..\README.md = ..\README.md
1311
EndProjectSection
1412
EndProject
13+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dtge9Testsite", "Dtge9Testsite\Dtge9Testsite.csproj", "{7C24F7A4-D89C-4E48-BC0C-CE8EA2674A10}"
14+
EndProject
1515
Global
1616
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1717
Debug|Any CPU = Debug|Any CPU
@@ -22,10 +22,10 @@ Global
2222
{D4318AFD-033C-46FC-9DD2-9FC57934CD2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
2323
{D4318AFD-033C-46FC-9DD2-9FC57934CD2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
2424
{D4318AFD-033C-46FC-9DD2-9FC57934CD2F}.Release|Any CPU.Build.0 = Release|Any CPU
25-
{D510AF65-1DCB-4A60-99DE-DA1D3B6E1AC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26-
{D510AF65-1DCB-4A60-99DE-DA1D3B6E1AC7}.Debug|Any CPU.Build.0 = Debug|Any CPU
27-
{D510AF65-1DCB-4A60-99DE-DA1D3B6E1AC7}.Release|Any CPU.ActiveCfg = Release|Any CPU
28-
{D510AF65-1DCB-4A60-99DE-DA1D3B6E1AC7}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{7C24F7A4-D89C-4E48-BC0C-CE8EA2674A10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{7C24F7A4-D89C-4E48-BC0C-CE8EA2674A10}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{7C24F7A4-D89C-4E48-BC0C-CE8EA2674A10}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{7C24F7A4-D89C-4E48-BC0C-CE8EA2674A10}.Release|Any CPU.Build.0 = Release|Any CPU
2929
EndGlobalSection
3030
GlobalSection(SolutionProperties) = preSolution
3131
HideSolutionNode = FALSE
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@using Our.Umbraco.DocTypeGridEditor9.Helpers
1+
@using Our.Umbraco.DocTypeGridEditor.Helpers
22
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<dynamic>
33
@inject DocTypeGridEditorHelper DocTypeGridEditorHelper
4-
@DocTypeGridEditorHelper.RenderDocTypeGridEditorItem(Html, Model)
4+
@DocTypeGridEditorHelper.RenderDocTypeGridEditorItem(Component, Html, Model)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
@using Our.Umbraco.DocTypeGridEditor9.Web.Extensions
2-
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Our.Umbraco.DocTypeGridEditor9.Web.Models.PreviewModel>
3-
@Html.RenderDocTypeGridEditorItem(Model.Item, Model.EditorAlias, Model.ViewPath, Model.PreviewViewPath, true)
1+
@using Our.Umbraco.DocTypeGridEditor.Helpers
2+
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Our.Umbraco.DocTypeGridEditor.Models.PreviewModel>
3+
@inject DocTypeGridEditorHelper DocTypeGridEditorHelper
4+
@DocTypeGridEditorHelper.RenderDocTypeGridEditorItem(Component, Html, Model.Item, Model.EditorAlias, Model.ViewPath, Model.PreviewViewPath, true)

src/Our.Umbraco.DocTypeGridEditor/Helpers/DocTypeGridEditorHelper.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Html;
2+
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.AspNetCore.Mvc.Rendering;
34
using Microsoft.AspNetCore.Mvc.ViewComponents;
45
using Microsoft.AspNetCore.Mvc.ViewEngines;
@@ -204,7 +205,8 @@ private string GetContentTypeAliasByGuid(Guid contentTypeGuid)
204205
}
205206

206207
public IHtmlContent RenderDocTypeGridEditorItem(
207-
IHtmlHelper helper,
208+
IViewComponentHelper helper,
209+
IHtmlHelper htmlHelper,
208210
dynamic model)
209211
{
210212
if (model.value != null)
@@ -216,38 +218,40 @@ public IHtmlContent RenderDocTypeGridEditorItem(
216218
string viewPath = model.editor.config.viewPath.ToString();
217219

218220
var content = ConvertValue(id, contentTypeAlias, value);
219-
return RenderDocTypeGridEditorItem(helper, content, editorAlias, viewPath);
221+
return RenderDocTypeGridEditorItem(helper, htmlHelper, content, editorAlias, viewPath);
220222
}
221223

222224
return null;
223225
}
224226

225227
public IHtmlContent RenderDocTypeGridEditorItem(
226-
IHtmlHelper helper,
228+
IViewComponentHelper helper,
229+
IHtmlHelper htmlHelper,
227230
IPublishedElement content,
228231
string editorAlias = "",
229232
string viewPath = "",
230233
string previewViewPath = "",
231234
bool isPreview = false)
232235
{
233236
if (content == null)
234-
return new HtmlString(string.Empty);
237+
return new HtmlString("<pre>content is null</pre>");
235238

236239
// get view path
237-
if (!TryGetViewPath(helper.ViewContext, editorAlias, content.ContentType.Alias, viewPath, previewViewPath, isPreview, out string fullViewPath))
240+
if (!TryGetViewPath(htmlHelper.ViewContext, editorAlias, content.ContentType.Alias, viewPath, previewViewPath, isPreview, out string fullViewPath))
238241
{
239-
return new HtmlString(string.Empty);
242+
return new HtmlString($"<pre>could not get viewpath. {editorAlias}, {content.ContentType.Alias}, {viewPath}, {previewViewPath}, {isPreview}, {fullViewPath}</pre>");
240243
}
241244

242-
var renderParams = new object[] { content, fullViewPath };
245+
var renderParams = new { model = content, viewPath = fullViewPath };
243246
var viewComponent = _options.DefaultDocTypeGridEditorViewComponent;
244247

245-
if (TryGetComponent(editorAlias, out ViewComponentDescriptor component) || TryGetComponent(content.ContentType.Alias, out component))
248+
if (!TryGetComponentName(new[] { editorAlias, content.ContentType.Alias }, out string componentName))
246249
{
247-
viewComponent = component.GetType();
250+
return new HtmlString($"<pre>could not get componentName. {editorAlias}, {content.ContentType.Alias}, {componentName}</pre>");
251+
248252
}
249253

250-
return helper.RenderComponentAsync(viewComponent, RenderMode.Static, renderParams).Result;
254+
return helper.InvokeAsync(componentName, renderParams).Result;
251255
}
252256

253257
private bool TryGetViewPath(ViewContext viewContext, string editorAlias, string contentTypeAlias, string viewPath, string previewViewPath, bool isPreview, out string fullViewPath)
@@ -292,13 +296,19 @@ private bool TryGetViewPath(ViewContext viewContext, string editorAlias, string
292296
return !fullViewPath.IsNullOrWhiteSpace();
293297
}
294298

295-
private bool TryGetComponent(string name, out ViewComponentDescriptor component)
299+
private bool TryGetComponentName(string[] names, out string componentName)
296300
{
297-
component = null;
298-
if (name.IsNullOrWhiteSpace()) return false;
299-
300-
component = _viewComponentSelector.SelectComponent($"{name}DocTypeGridEditorViewComponent");
301-
return component != null;
301+
componentName = "";
302+
foreach (var name in names)
303+
{
304+
Console.WriteLine($"getting component name for {name}");
305+
if (_viewComponentSelector.SelectComponent($"{name}DocTypeGridEditor") != null)
306+
{
307+
componentName = $"{name}DocTypeGridEditor";
308+
return true;
309+
}
310+
}
311+
return false;
302312
}
303313

304314
public static bool ViewExists(ViewContext viewContext, string viewName)

src/Our.Umbraco.DocTypeGridEditor/ViewComponents/DocTypeGridEditorViewComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Our.Umbraco.DocTypeGridEditor.ViewComponents
1212
{
1313
public class DocTypeGridEditorViewComponent : ViewComponent
1414
{
15-
public IViewComponentResult Invoke(IPublishedElement model, string viewPath)
15+
public IViewComponentResult Invoke(dynamic model, string viewPath)
1616
{
1717
return View(viewPath, model);
1818
}

0 commit comments

Comments
 (0)