title: asp.net core how can i post category and subcategory from table row
body: <p>Here is my Category.cs,</p>
  <pre><code>public class Category
  {
      public int Id { get; set; }
      [Required, StringLength(100),Display(Name=&quot;Category Name&quot;)]
      public string Name { get; set; }
      [Display(Name=&quot;Category Description&quot;)]
      public string Description { get; set; }
      public bool IsActive { get; set; }
      public IList&lt;Style&gt; Style { get; set; }
      public IList&lt;SubCategory&gt; SubCategories { get; set; }
  }
  public class SubCategory
  {
      public int Id { get; set; }
      public string Value { get; set; }
  }
  public class IndexModel : PageModel
  {
      [Microsoft.AspNetCore.Mvc.BindProperty]
      public Category ViewModel { get; set; }
      public void OnGet()
      {
          // You can fill your ViewModel property here.
      }
      public void OnPost()
      {
          // You can read your posted ViewModel property here.
      }
  }
  </code></pre>
  <p>And DbContext.cs</p>
  <pre><code>public DbSet&lt;Category&gt; Categories { get; set; }
  public DbSet&lt;SubCategory&gt; SubCategory { get; set; }
  </code></pre>
  <p>And AddCategory.cshtml</p>
  <pre><code>@model UretimPerformans.Entity.Admin.IndexModel
  @{
  ViewData[&quot;Title&quot;] = Localizer[&quot;addca&quot;];
  Layout = &quot;~/Views/Shared/_AdminLayout.cshtml&quot;;
  }
  
  @section Scripts{
      &lt;link href=&quot;~/dist/plugins/bootstrap-switch/css/bootstrap3/bootstrap-switch.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
      &lt;script src=&quot;~/js/jquery-3.5.1.js&quot;&gt;&lt;/script&gt;
      &lt;style&gt;
          td, tr {
              text-align: center;
              vertical-align: middle;
          }
      &lt;/style&gt;
  }
  
      @section Scripts2{
      &lt;script src=&quot;~/dist/plugins/bootstrap-switch/js/bootstrap-switch.js&quot;&gt;&lt;/script&gt;
  
      &lt;script&gt;
          $(&quot;#btnSearch&quot;).click(function () {
              var t = null;
              t = $(&quot;#tab_logic tbody tr&quot;).get().map(function (tr) {
                  return $(tr).find(&quot;input&quot;).get().map(function (input) {
                      return input.value
                  })
              })
  
              $(&quot;#regTitle&quot;).html(t);
          });
      &lt;/script&gt;
  
      &lt;script&gt;
          $(document).ready(function () {
              var i = 1;
              $(&quot;#add_row&quot;).click(function () {
                  $('#addr' + i).html(&quot;&lt;td&gt;&quot; + (i + 1) + &quot;&lt;/td&gt;&lt;td&gt;&lt;input name='name&quot; + i + &quot;' required type='text' placeholder='SubCategory-&quot; + i + &quot;' class='form-control input-md'  /&gt; &lt;/td&gt;&lt;td&gt;&lt;input  name='mail&quot; + i + &quot;' type='checkbox' placeholder='IsActive'  class='form-control'&gt;&lt;/td&gt;&quot;);
  
                  $('#tab_logic').append('&lt;tr id=&quot;addr' + (i + 1) + '&quot;&gt;&lt;/tr&gt;');
                  i++;
              });
              $(&quot;#delete_row&quot;).click(function () {
                  if (i &gt; 1) {
                      $(&quot;#addr&quot; + (i - 1)).html('');
                      i--;
                  }
              });
          });
  
  
          $(function () {
  
              $(&quot;input[data-bootstrap-switch]&quot;).each(function () {
                  $(this).bootstrapSwitch('state', $(this).prop('checked'));
              });
  
          })
      &lt;/script&gt;
  }
  &lt;section class=&quot;content-header&quot;&gt;
      &lt;div class=&quot;container-fluid&quot;&gt;
          &lt;div class=&quot;row mb-2&quot;&gt;
              &lt;div class=&quot;col-sm-6&quot;&gt;
                  &lt;h1&gt;@Localizer[&quot;addca&quot;]&lt;/h1&gt;
              &lt;/div&gt;
              &lt;div class=&quot;col-sm-6&quot;&gt;
                  &lt;ol class=&quot;breadcrumb float-sm-right&quot;&gt;
                      &lt;li class=&quot;breadcrumb-item&quot;&gt;&lt;a asp-action=&quot;Index&quot; asp-controller=&quot;Home&quot;&gt;@Localizer[&quot;anasayfa&quot;]&lt;/a&gt;&lt;/li&gt;
                      &lt;li class=&quot;breadcrumb-item&quot;&gt;&lt;a asp-action=&quot;Index&quot; asp-controller=&quot;Catalog&quot;&gt;@Localizer[&quot;cat&quot;]&lt;/a&gt;&lt;/li&gt;
                      &lt;li class=&quot;breadcrumb-item&quot;&gt;&lt;a asp-action=&quot;Categories&quot; asp-controller=&quot;Catalog&quot;&gt;@Localizer[&quot;ca&quot;]&lt;/a&gt;&lt;/li&gt;
                      &lt;li class=&quot;breadcrumb-item active&quot;&gt;@Localizer[&quot;addca&quot;]&lt;/li&gt;
                  &lt;/ol&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  &lt;section class=&quot;content&quot;&gt;
      &lt;div class=&quot;card&quot;&gt;
          &lt;div class=&quot;card-body&quot;&gt;
              &lt;form asp-action=&quot;AddCategories&quot; asp-controller=&quot;Catalog&quot; method=&quot;post&quot;&gt;
                  &lt;div class=&quot;form-group row&quot;&gt;
                      &lt;span class=&quot;col-md-3&quot;&gt;@Localizer[&quot;name&quot;]&lt;/span&gt;
                      &lt;div class=&quot;col-md-9&quot;&gt;
                          &lt;input type=&quot;text&quot; class=&quot;form-control&quot; asp-for=&quot;ViewModel.Name&quot; required /&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;form-group row&quot;&gt;
                      &lt;span class=&quot;col-md-3&quot;&gt;@Localizer[&quot;desc&quot;]&lt;/span&gt;
                      &lt;div class=&quot;col-md-9&quot;&gt;
                          &lt;textarea type=&quot;text&quot; id=&quot;regTitle&quot; class=&quot;form-control&quot; asp-for=&quot;ViewModel.Description&quot; required&gt;&lt;/textarea&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;form-group row&quot;&gt;
                      &lt;span class=&quot;col-md-3&quot;&gt;@Localizer[&quot;aktif&quot;]&lt;/span&gt;
                      &lt;div class=&quot;col-md-9&quot;&gt;
                          &lt;input type=&quot;checkbox&quot; asp-for=&quot;ViewModel.IsActive&quot; checked data-bootstrap-switch data-off-color=&quot;danger&quot; data-on-color=&quot;success&quot;&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;fieldset class=&quot;module&quot;&gt;
                      &lt;h4&gt;@Localizer[&quot;alt&quot;]&lt;/h4&gt;
  
                      &lt;button id=&quot;btnSearch&quot;&gt;Click&lt;/button&gt;
                      &lt;a id=&quot;add_row&quot; class=&quot;btn btn-default pull-left&quot;&gt;Add Row&lt;/a&gt;
                      &lt;a id='delete_row' class=&quot;pull-right btn btn-default&quot;&gt;Delete Row&lt;/a&gt;
                      &lt;hr /&gt;
                      &lt;div class=&quot;row clearfix&quot;&gt;
                          &lt;div class=&quot;col-md-12 column&quot;&gt;
                              &lt;table class=&quot;table table-bordered table-hover&quot; id=&quot;tab_logic&quot;&gt;
                                  &lt;thead&gt;
                                      &lt;tr&gt;
                                          &lt;th class=&quot;text-center&quot;&gt;
                                              #
                                          &lt;/th&gt;
                                          &lt;th class=&quot;text-center&quot;&gt;
                                              SubCategoryName
                                          &lt;/th&gt;
                                          &lt;th class=&quot;text-center&quot;&gt;
                                              IsActive
                                          &lt;/th&gt;
                                      &lt;/tr&gt;
                                  &lt;/thead&gt;
                                  &lt;tbody&gt;                                    
                                          &lt;tr id='addr0'&gt;
                                              &lt;td&gt;1&lt;/td&gt;                                            
                                              &lt;td&gt;
                                                  &lt;input asp-for=&quot;ViewModel.SubCategories[1].Value&quot; /&gt;
                                                  @Model.ViewModel.SubCategories[1].Value
                                              &lt;/td&gt;
                                          &lt;/tr&gt;
                                      
                                      &lt;tr id='addr0'&gt;
                                          &lt;td&gt;
                                              1
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;input type=&quot;text&quot; name='name0' placeholder='Name' class=&quot;form-control&quot; /&gt;
                                          &lt;/td&gt;
                                          &lt;td&gt;
                                              &lt;input type=&quot;checkbox&quot; name='mail0' placeholder='Mail' class=&quot;form-control&quot; /&gt;
                                          &lt;/td&gt;
                                      &lt;/tr&gt;
                                      &lt;tr id='addr1'&gt;&lt;/tr&gt;
                                  &lt;/tbody&gt;
                              &lt;/table&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/fieldset&gt;
                  &lt;div class=&quot;card-footer&quot;&gt;
                      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;@Localizer[&quot;kaydet&quot;]&lt;/button&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/section&gt;
  </code></pre>
  <p>And CategoryController,</p>
  <pre><code>        [HttpGet]
          public IActionResult AddCategories()
          {
              return View();
          }
          [HttpPost]
          public IActionResult AddCategories(Category entity)
          {
              if (ModelState.IsValid)
              {
                  categoryRepository.AddCategory(entity);
                  TempData[&quot;success&quot;] = &quot;Başarıyla kaydedildi.&quot;;
                  return RedirectToAction(&quot;Categories&quot;);
              }
              else
              {
                  return View(entity);
              }
          }
  </code></pre>
  <p>My codes are as above. What I want to do is add subcategories from the table at the bottom of the category when adding a category. but I could not set up the model. and I could not write the post method and reading method in indexmodel.
  Please can you help me?</p>
  <p>Thank you
  Kind Regards</p>