title: How to use asp-for to get value from a model and set value to another model?
body: <p>I'm trying to build one view that includes all (Create, Edit, Delete, and Index) in one View which is Index.</p>
  <p><strong>The problem is with Editing. Always returns null to the controller as shown in the gif.</strong></p>
  <blockquote>
  <p>I know what is the problem but I can't solve it. <em>The explanation at the end of the question after the hr line</em></p>
  </blockquote>
  <p><a href="https://i.stack.imgur.com/c9gsZ.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c9gsZ.gif" alt="enter image description here" /></a></p>
  <p>I have Model and ViewModel as follows.</p>
  <p><strong>The Model BootstrapCategory</strong></p>
  <pre><code>public class BootstrapCategory
  {
      [Key]
      public Guid Id { get; set; }
  
      [MaxLength(20)]
      [Required]
      public string Category { get; set; }
  }
  </code></pre>
  <p><strong>The ViewModel VMBPCategoris</strong></p>
  <pre><code>public class VMBPCategoris
  {
      public List&lt;BootstrapCategory&gt; bootstrapCategories { get; set; }
  
      public Guid Id { get; set; }
      public string Category { get; set; }
  }
  </code></pre>
  <p><strong>The View</strong></p>
  <blockquote>
  <p>Note: Edit not by the usual button in the table it instead by another
  button as shown in the gif</p>
  </blockquote>
  <pre><code>@model VMBPCategoris
  
          @foreach (var item in Model.bootstrapCategories)
          {
              &lt;tr&gt;
                  &lt;td&gt;
  
                      &lt;form asp-action=&quot;Edit&quot; method=&quot;post&quot;&gt;
                          &lt;input type=&quot;hidden&quot; asp-for=&quot;@item.Id&quot; /&gt;
                          &lt;div class=&quot;@item.Id d-none&quot;&gt;
                              &lt;div class=&quot;input-group&quot;&gt;
                                  &lt;input id=&quot;btnGroupEdit&quot; type=&quot;submit&quot; value=&quot;Save&quot; class=&quot;input-group-text btn btn-primary&quot; /&gt;
                                  &lt;input asp-for=&quot;@item.Category&quot; class=&quot;form-control&quot; aria-label=&quot;Input group example&quot; aria-describedby=&quot;btnGroupEdit&quot;&gt;
                              &lt;/div&gt;
                              &lt;span asp-validation-for=&quot;@item.Category&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                          &lt;/div&gt;
                      &lt;/form&gt;
  
                      &lt;div class=&quot;@item.Id&quot;&gt;
                          @Html.DisplayFor(modelItem =&gt; item.Category)
                      &lt;/div&gt;
  
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;a href=&quot;@item.Id&quot; class=&quot;ToggleEdit&quot;&gt;Edit&lt;/a&gt; |
                      &lt;a asp-action=&quot;Details&quot; asp-route-id=&quot;@item.Id&quot;&gt;Details&lt;/a&gt; |
                      &lt;a asp-action=&quot;Delete&quot; asp-route-id=&quot;@item.Id&quot;&gt;Delete&lt;/a&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
  </code></pre>
  <p><strong>The Controller</strong></p>
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public async Task&lt;IActionResult&gt; Edit([Bind(&quot;Id,Category&quot;)] BootstrapCategory bootstrapCategory)
  {
  
      _context.Update(bootstrapCategory);
      await _context.SaveChangesAsync();
  
      return RedirectToAction(nameof(Index));
      //return View(vMBPCategoris);
  }
  </code></pre>
  <hr />
  <p><strong>In the ViewModel VMBPCategoris</strong></p>
  <p>The problem, I get the list of Category values from this list</p>
  <pre><code> public List&lt;BootstrapCategory&gt;bootstrapCategories { get; set; }
  </code></pre>
  <p>on edit, I set a new value to one member in the list, not to this</p>
  <pre><code> public Guid Id { get; set; }
   public string Category { get; set; }
  </code></pre>
  <p>So that It returns Null</p>
  <blockquote>
  <p>However, if I change <strong>asp-for=&quot;@item.Category&quot;</strong>  to<br />
  <strong>asp-for=&quot;Category&quot;</strong> the input gets empty value, but it returns the edited value not empty</p>
  </blockquote>
  <p>**Here is my question &gt;&gt; can I use asp-for to get value from</p>
  <pre><code>  public List&lt;BootstrapCategory&gt; bootstrapCategories { get; set; }
  </code></pre>
  <p><strong>and set value to</strong></p>
  <pre><code>  public Guid Id { get; set; }
    public string Category { get; set; }
  </code></pre>
  <p>Because,<br />
  using <strong>asp-for=&quot;@item.Category&quot;</strong> is beneficial to get category values but not for setting a value.<br />
  And using <strong>asp-for=&quot;Category&quot;</strong> is beneficial to set category value but not for getting a value.</p>