title: Use nameof() to get a attributepath for ModelState.AddModelError()
body: <p>I like to use nameof() in my ASP.NET Core application when setting mvc errors using ModelState.AddModelError(). This reduces hidden errors since I pass the name of an attribute here. Compared to a string, this has the benefit that a compiler error is thrown when the attribute was renamed. </p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>class MyModel{
      public string Name{get;set;}
  }
  public class MyController:Controller{
      public IActionResult Test(MyModel model) {
          // Will hiddenly fail when "Name" is renamed to something else
          ModelState.AddModelError("Name", "The Name field is required!");
  
          // Better: Using the auto renaming feature of VS, this will be updated if "Name" was renamed
          ModelState.AddModelError(nameof(model.Name), "The Name field is required!");
      }
  }
  </code></pre>
  
  <p>This works, until I have another entity:</p>
  
  <pre><code>class MyModel{
      public string Name{get;set;}
      public MyOtherModel OtherModel{get;set;}
  }
  class MyOtherModel{
      public string OtherName{get;set;}
  }
  </code></pre>
  
  <p>Now I want to get the name of <code>OtherModel</code>. The problem: ASP.NET requires BaseClass.ChildClass pattern here, so <code>OtherModel.OtherName</code> in this example. Using the same logic like above:</p>
  
  <pre><code>public class MyController:Controller{
      public IActionResult Test(MyModel model) {
          ModelState.AddModelError(nameof(model.MyOtherModel.OtherName), "The Name field is required!");
      }
  }
  </code></pre>
  
  <p>will just give me the name of the child attribute, so <code>OtherName</code> in this example. But I need <code>OtherModel.OtherName</code>. Is there a clean way to get this without building the string itself? </p>
  
  <p>This would be possible doing something like this: </p>
  
  <pre><code>string actionName = nameof(model.MyOtherModel) + "." + nameof(model.MyOtherModel.OtherName);
  </code></pre>
  
  <p>But not a very clean and intuitive way imho. </p>