title: How can I get the PropertyName of the ContainerMetadata when trying to navigate the ModelMetadata with C#?
body: <p>I have a project written using <code>C#</code> on the top of an ASP.NET Core 5 framework.</p>
  <p>I am trying use Razor engine to manually render the <code>ModelMetadata</code> content. The <code>ModelMetadata</code> gives me a property called <code>ContainerMetadata</code> which tell me into about the parent <code>ModelMetadata</code> in the case of a complex-type property found on the parent ModelMetadata.</p>
  <p>However, the <code>PropertyName</code> property on the <code>ContinerMetadata</code> is always null.</p>
  <p>I have the following view-model. The <code>MainViewModel</code> bellow is passed from the controller to the view using the <code>View()</code> method.</p>
  <pre><code>public class MainViewModel
  {
      public int ID { get; set; }
  
      public class CellViewModel { get; set; }
  }
  
  public class CellViewModel
  {
      public string Name { get; set; }
  }
  </code></pre>
  <p>Now in the razor view, I am access the <code>ModelMetadata</code> for the passed model using <code>Html.ViewData.ModelMetadata</code></p>
  <p>My goal is to be able to render the HTML manually using extension method. To get the <code>ModelMetadata</code> for each property I want to render, I use the following function</p>
  <pre><code>public static IHtmlContent GetHtml(this IHtmlHelper html, ModelMetadata metadata)
  {
      HtmlContentBuilder code = new HtmlContentBuilder();
  
      IEnumerable&lt;ModelMetadata&gt; elements = metadata.Properties.Where(metadata =&gt; metadata.ShowForDisplay);
  
      foreach(ModelMetadata element in elements)
      {
          if(element.IsClass) 
          {   
              // When this code is called, each proeprty on the element.ModelType has ContainerModelmetadata which is element
              // However, when I access ContainerModelmetadata.PropertyName I get null.
              var subCode = html.GetHtml(element);
  
              code.AppendHtml(subCode);
  
              continue;
          }
  
          code.AppendHtml(html.Editor(element.propertyName));
      }
  
      return code;
  }
  </code></pre>
  <p>As you can see in the comment above, I can't seem to access the property name of the parent.</p>
  <p>So work around this problem, I can passing the parent each time like this</p>
  <pre><code>public static IHtmlContent GetHtml(this IHtmlHelper html, ModelMetadata metadata, string parentName = null)
  {
      HtmlContentBuilder code = new HtmlContentBuilder();
  
      IEnumerable&lt;ModelMetadata&gt; elements = metadata.Properties.Where(metadata =&gt; metadata.ShowForDisplay);
  
      foreach(ModelMetadata element in elements)
      {
          if(element.IsClass) 
          {   
              // When this code is called, each proeprty on the element.ModelType has ContainerModelmetadata which is element
              // However, when I access ContainerModelmetadata.PropertyName I get null.
              var subCode = html.GetHtml(element, element.Name);
  
              code.AppendHtml(subCode);
  
              continue;
          }
  
          string expression = element.propertyName;
  
          if(!string.IsNullOrEmpty(parentName)) 
          {
              expression = $&quot;{parentName}.{element.PropertyName}&quot;;
          }
  
          code.AppendHtml(html.Editor(expression));
      }
  
      return code;
  }
  </code></pre>
  <p>As you can see above, I have to pass the <code>parentName</code> to each level so I can get the correct expression. The problem here is that in my code, I have deeper levels, and this required for me to pass the parentName to each function. I am hoping to avoid having to pass both the Modelmetadata of the property in addition to the parentName. I am hoping to be able to get the parentName from the given <code>Modelmetadata</code> since I already have the <code>ContainerModelmetadata</code>.</p>
  <p>How can I correctly access the value of the <code>PropertyName</code> from the <code>ContinerMetadata</code> when <code>ContinerMetadata</code> is not null?</p>