diff --git a/src/Framework/Framework/Binding/BindingHelper.cs b/src/Framework/Framework/Binding/BindingHelper.cs
index 46ac3becf..bc0c1c87c 100644
--- a/src/Framework/Framework/Binding/BindingHelper.cs
+++ b/src/Framework/Framework/Binding/BindingHelper.cs
@@ -101,7 +101,30 @@ internal static (int stepsUp, DotvvmBindableObject target) FindDataContextTarget
{
// only count changes which are visible client-side
// server-side context are not present in the client-side stack at all, so we need to skip them here
- changes++;
+
+ // don't count changes which only extend the data context, but don't nest it
+
+ var isNesting = ancestorContext.IsAncestorOf(lastAncestorContext);
+ if (isNesting)
+ {
+ changes++;
+ }
+#if DEBUG
+ else if (!lastAncestorContext.DataContextType.IsAssignableFrom(ancestorContext.DataContextType))
+ {
+ // this should not happen - data context type should not randomly change without nesting.
+ // we change data context stack when we get into different compilation context - a markup control
+ // but that will be always the same viewmodel type (or supertype)
+
+ var previousAncestor = control.GetAllAncestors(includingThis: true).TakeWhile(aa => aa != a).LastOrDefault();
+ var config = (control.GetValue(Internal.RequestContextProperty) as Hosting.IDotvvmRequestContext)?.Configuration;
+ throw new DotvvmControlException(
+ previousAncestor ?? a,
+ $"DataContext type changed from '{lastAncestorContext.DataContextType.ToCode()}' to '{ancestorContext.DataContextType.ToCode()}' without nesting. " +
+ $"{previousAncestor?.DebugString(config)} has DataContext: {lastAncestorContext}, " +
+ $"{a.DebugString(config)} has DataContext: {ancestorContext}");
+ }
+#endif
lastAncestorContext = ancestorContext;
}
diff --git a/src/Framework/Framework/Compilation/ControlTree/DataContextStack.cs b/src/Framework/Framework/Compilation/ControlTree/DataContextStack.cs
index 4eab7fd45..8420ee3d5 100644
--- a/src/Framework/Framework/Compilation/ControlTree/DataContextStack.cs
+++ b/src/Framework/Framework/Compilation/ControlTree/DataContextStack.cs
@@ -110,6 +110,21 @@ public IEnumerable
diff --git a/src/Tests/ControlTests/testoutputs/MarkupControlTests.MarkupControl_CommandInRepeater.html b/src/Tests/ControlTests/testoutputs/MarkupControlTests.MarkupControl_CommandInRepeater.html new file mode 100644 index 000000000..76be88751 --- /dev/null +++ b/src/Tests/ControlTests/testoutputs/MarkupControlTests.MarkupControl_CommandInRepeater.html @@ -0,0 +1,16 @@ + +
+
+