title: MVC Core. Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions
body: <p>I am passing in my view this model</p>
  
  <pre><code>public class BlogViewModel
  {
      public List&lt;CommentModel&gt; commentModels { get; set; }
      public BlogPostLayoutModel blogPostLayoutModel { get; set; }
      public CommentModel commentModel { get; set; }
      public int pageNumber { get; set; }
  }
  </code></pre>
  
  <p>CommentModel class</p>
  
  <pre><code> public class CommentModel
  {
      public int IDComment { get; set; }
      public int IDPost { get; set; }
  
      [Required()]
      public string Username { get; set; }
      [Required()]
      public string Content { get; set; }
  
      public static CommentModel CommentToCommentModel(Comment comment)
      {
          CommentModel commentModel = new CommentModel();
          commentModel.IDComment = comment.IDComment;
          commentModel.IDPost = comment.IDPost;
          commentModel.Username = comment.Username;
          commentModel.Content = comment.Content;
          return commentModel;
      }
  
      public static List&lt;CommentModel&gt; CommentsToCommentModels(List&lt;Comment&gt; comments)
      {
          List&lt;CommentModel&gt; commentModels = new List&lt;CommentModel&gt;();
          foreach (var comment in comments)
          {
              CommentModel commentModel = new CommentModel();
              commentModel.IDComment = comment.IDComment;
              commentModel.IDPost = comment.IDPost;
              commentModel.Username = comment.Username;
              commentModel.Content = comment.Content;
              commentModels.Add(commentModel);
          }
          return commentModels;
      }
  }
  </code></pre>
  
  <p>And my view, where the error is thrown (fifth line)</p>
  
  <pre><code>&lt;form asp-action="AddComment"&gt;
          &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
          @{ var pageNumber = Model.pageNumber;
          @Html.HiddenFor(x =&gt; pageNumber)
              int IDPost = Model.blogPostLayoutModel.IDPost;
          @Html.HiddenFor(x =&gt; x.commentModel.IDPost == IDPost)
          }
  
          &lt;div class="row"&gt;
              &lt;div class="form-group col-md-4"&gt;
                  &lt;label asp-for="commentModel.Username" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="commentModel.Username" class="form-control" /&gt;
                  &lt;span asp-validation-for="commentModel.Username" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="row"&gt;
              &lt;div class="form-group col-md-12"&gt;
                  &lt;label class="control-label"&gt;Comment&lt;/label&gt;
                  &lt;textarea asp-for="commentModel.Content" class="form-control"&gt;&lt;/textarea&gt;
                  &lt;span asp-validation-for="commentModel.Content" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group col-md-4"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>I have searched for the error, but seems like it's mostly people trying to return a value from a method and getting told to pass the value to a local variable and then put that local variable inside the tag helper, but that doesn't seem to be the case here.</p>
  
  <p>And also  blogPostLayoutModel.IDPost is defined as </p>
  
  <pre><code> public int IDPost { get; set; }
  </code></pre>
  
  <p>EDIT: My commentModel was null, so that was the problem.</p>
  
  <p>Working form</p>
  
  <pre><code>&lt;form asp-action="AddComment"&gt;
          &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
          @{
          @Html.HiddenFor(x =&gt; Model.pageNumber)
              Model.commentModel.IDPost = Model.blogPostLayoutModel.IDPost;
          @Html.HiddenFor(x =&gt; x.commentModel.IDPost)
          }
          &lt;div class="row"&gt;
              &lt;div class="form-group col-md-4"&gt;
                  &lt;label asp-for="commentModel.Username" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="commentModel.Username" class="form-control" /&gt;
                  &lt;span asp-validation-for="commentModel.Username" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="row"&gt;
              &lt;div class="form-group col-md-12"&gt;
                  &lt;label class="control-label"&gt;Comment&lt;/label&gt;
                  &lt;textarea asp-for="commentModel.Content" class="form-control"&gt;&lt;/textarea&gt;
                  &lt;span asp-validation-for="commentModel.Content" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group col-md-4"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>