Note that there are some explanatory texts on larger screens.

plurals
  1. POC# MVC Syntax - Recursive seeking in a template lambda (EditorFor)
    text
    copied!<p>I have a recursive model. </p> <pre><code>public class Mapping { public string Value { get; set; } public List&lt;Mapping&gt; ChildMappings { get; set; } } </code></pre> <p>I have an EditorTemplate for the Mapping class. Normal rendering via EditorFor( m => m.ChildMappings) works fine.</p> <p>Based on an array parameter (2,3), I'd like to seek a specific descendant node. (Mapping.ChildMappings[2].ChildMappings[3] for example).</p> <p>I've tried calling a local method within the lambda, which gave me an error about indexers and all the things I can't do in a template.</p> <pre><code>@Html.EditorFor(m =&gt; RecurseMappings(m.BodyMappings, indexes)) @functions{ private Mapping RecurseMappings(List&lt;Mapping&gt; mappings, int[] indexes) { Mapping mapping = new Mapping(); foreach (int index in indexes) { mapping = mappings[index]; if (mapping.ChildMappings == null) { mapping.ChildMappings = new List&lt;RequestMapping&gt;(); } mappings = mappings[index].ChildMappings; } return mappings[mappings.Count - 1]; } </code></pre> <p>For the moment I've fallen back on a switch to at least facilitate a hard coded number of nested levels, like this:</p> <pre><code> string[] tokens = Model.ChildIndex.Split(','); int[] indexes = Array.ConvertAll&lt;string, int&gt;(tokens, int.Parse); switch (indexes.GetLength(0)) { case 1: { var last = Model.BodyMappings[indexes[0]].ChildMappings.Count - 1; @Html.EditorFor(m =&gt; m.BodyMappings[indexes[0]].ChildMappings[last]) break; } case 2: { var last = Model.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings.Count - 1; @Html.EditorFor(m =&gt; m.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[last]) break; } case 3: { var last = Model.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings.Count - 1; @Html.EditorFor(m =&gt; m.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[last]) break; } case 4: { var last = Model.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[indexes[3]].ChildMappings.Count - 1; @Html.EditorFor(m =&gt; m.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[indexes[3]].ChildMappings[last]) break; } case 5: { var last = Model.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[indexes[3]].ChildMappings[indexes[4]].ChildMappings.Count - 1; @Html.EditorFor(m =&gt; m.BodyMappings[indexes[0]].ChildMappings[indexes[1]].ChildMappings[indexes[2]].ChildMappings[indexes[3]].ChildMappings[indexes[4]].ChildMappings[last]) break; } default: break; } </code></pre> <p>So, Is there a way to wrap up what I have working in a package more like my failed first attempt? I've also tried a lambda code block, only to be denied because it can't be converted to an expression tree.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload