Skip to content
This repository was archived by the owner on Apr 6, 2022. It is now read-only.

Commit 49be9f8

Browse files
committed
Move context to own model and add as property on new BlockListItemViewPage
1 parent fb1f411 commit 49be9f8

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

src/Our.Umbraco.BlockListMvc/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ namespace Our.Umbraco.BlockListMvc
88
{
99
public class Constants
1010
{
11-
public const string BlockListViewDataKey = "Our.Umbraco.BlockListMvc.BlockList";
11+
public const string BlockListItemContextViewDataKey = "Our.Umbraco.BlockListMvc.BlockListItemContext";
1212
}
1313
}

src/Our.Umbraco.BlockListMvc/Controllers/BlockListMvcSurfaceController.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Our.Umbraco.BlockListMvc.Models;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -21,6 +22,11 @@ public TModel Model
2122
get { return (TModel)ControllerContext.RouteData.Values["blockListItem"]; }
2223
}
2324

25+
public BlockListItemContext BlockListItemContext
26+
{
27+
get { return (BlockListItemContext)ControllerContext.RouteData.Values["blockListItemContext"]; }
28+
}
29+
2430
public string ViewPath
2531
{
2632
get { return ControllerContext.RouteData.Values["blockListItemViewPath"] as string ?? string.Empty; }
@@ -31,6 +37,11 @@ public string ContentTypeAlias
3137
get { return ControllerContext.RouteData.Values["blockListItemContentTypeAlias"] as string ?? string.Empty; }
3238
}
3339

40+
protected void SetBlockListItemContext()
41+
{
42+
ViewData[Constants.BlockListItemContextViewDataKey] = BlockListItemContext;
43+
}
44+
3445
protected PartialViewResult CurrentPartialView(object model = null)
3546
{
3647
if (model == null)
@@ -48,13 +59,14 @@ protected PartialViewResult CurrentPartialView(object model = null)
4859

4960
protected override PartialViewResult PartialView(string viewName, object model)
5061
{
62+
SetBlockListItemContext();
63+
5164
if (string.IsNullOrWhiteSpace(ViewPath) == false)
5265
{
5366
var viewPath = GetFullViewPath(viewName, ViewPath);
5467
if (ViewEngines.Engines.FindPartialView(ControllerContext, viewPath).View != null)
5568
return base.PartialView(viewPath, model);
5669
}
57-
5870
return base.PartialView(viewName, model);
5971
}
6072

src/Our.Umbraco.BlockListMvc/Helpers/RenderHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ internal static BlockListItemRendering GetBlockListItemData(BlockListItem item,
7070

7171
rendering.RouteValues = new
7272
{
73-
blockListItem = new BlockListItemWithContext(item, list),
73+
blockListItem = item,
74+
blockListItemContext = new BlockListItemContext(item, list),
7475
blockListItemContentTypeAlias = contentTypeAlias,
7576
blockListItemViewPath = viewPath
7677
};

src/Our.Umbraco.BlockListMvc/Models/BlockListItemContext.cs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
using System.Threading.Tasks;
66
using Umbraco.Core;
77
using Umbraco.Core.Models.Blocks;
8-
using Umbraco.Core.Models.PublishedContent;
98

109
namespace Our.Umbraco.BlockListMvc.Models
1110
{
12-
public class BlockListItemWithContext : BlockListItem
11+
public class BlockListItemContext
1312
{
14-
public BlockListItemWithContext(BlockListItem item, BlockListModel list) : base(item.ContentUdi, item.Content, item.SettingsUdi, item.Settings)
13+
public BlockListItemContext(BlockListItem item, BlockListModel list)
1514
{
1615
Item = item;
1716
List = list;
@@ -35,26 +34,4 @@ public BlockListItemWithContext(BlockListItem item, BlockListModel list) : base(
3534
private BlockListItem _nextBlock;
3635
public BlockListItem NextBlock => _nextBlock ?? (_nextBlock = List != null && !IsLast ? List[Index + 1] : null);
3736
}
38-
39-
public class BlockListItemWithContext<T> : BlockListItemWithContext
40-
where T : IPublishedElement
41-
{
42-
public BlockListItemWithContext(BlockListItem item, BlockListModel list) : base(item, list)
43-
{
44-
45-
}
46-
public new T Content { get; }
47-
}
48-
49-
public class BlockListItemWithContext<TContent, TSettings> : BlockListItemWithContext
50-
where TContent : IPublishedElement
51-
where TSettings : IPublishedElement
52-
{
53-
public BlockListItemWithContext(BlockListItem item, BlockListModel list) : base(item, list)
54-
{
55-
56-
}
57-
public new TContent Content { get; }
58-
public new TSettings Settings { get; }
59-
}
6037
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Our.Umbraco.BlockListMvc.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Umbraco.Web.Mvc;
8+
9+
namespace Our.Umbraco.BlockListMvc.Mvc
10+
{
11+
public abstract class BlockListItemViewPage<T> : UmbracoViewPage<T>
12+
{
13+
private BlockListItemContext _blockContext;
14+
public BlockListItemContext BlockContext => _blockContext ?? (_blockContext = ViewData[Constants.BlockListItemContextViewDataKey] as BlockListItemContext);
15+
16+
protected override void InitializePage()
17+
{
18+
base.InitializePage();
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)