title: How do you connect a POST to a different razor page loaded via AJAX into a modal popup?
body: <p>Edit: Have marked up where the error in the original code was stopping this from working.</p>
  
  <p>I can find plenty of info and examples of this on MVC, but doesn't seem to apply for Razor Pages?</p>
  
  <p>Simple scenario: I have a page (FooList) showing a list of Foo items. Each has an Edit button. This opens a modal popup with the layout (and data) coming from a second page (FooEdit).</p>
  
  <p>The Edit form appears and populates fine, but I can't work out how to get it to post the data back to the FooEdit code behind?</p>
  
  <p>List page, FooList.cshtml</p>
  
  <pre><code>@page
  @model Pages.FooListModel
  
  &lt;table&gt;
      @foreach (var item in Model.FooListVM)
      {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Name)
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;a onclick="openModal(@item.ID);"&gt;Edit&lt;/a&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  &lt;/table&gt;
  
  &lt;div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"&gt;
      &lt;div class="modal-dialog modal-lg modal-dialog-centered" role="document"&gt;
          &lt;div class="modal-content"&gt;
              &lt;div class="modal-header border-bottom-0"&gt;
                  &lt;h5 class="modal-title" id="exampleModalLabel"&gt;Edit Foo&lt;/h5&gt;
                  &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;
                      &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
                  &lt;/button&gt;
              &lt;/div&gt;
              &lt;form&gt;   &lt;---- Edit: ** This shouldn't be here **
                  &lt;div class="modal-body"&gt;
                  &lt;/div&gt;
              &lt;/form&gt;   &lt;---- Edit
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;script&gt;
      function openModal(i) {
          $.get("FooEdit?id="+i,
              null,
              data =&gt; {
                  $("#editModal").modal("show");
                  $("#editModal .modal-body").html(data);
              });
      };
  &lt;/script&gt;
  </code></pre>
  
  <p>Code behind, FooList.cshtml.cs</p>
  
  <pre><code>public class FooListModel : PageModel
  {
      public IList&lt;FooListVM&gt; FooListVM { get; set; }
  
      public void OnGet()
      {
          FooListVM = new List&lt;FooListVM&gt;
          {
              new FooListVM { ID = 1, Name = "Foo 1" },
              new FooListVM { ID = 2, Name = "Foo2" }
          };
      }
  }
  
  public class FooListVM
  {
      public int ID { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Second page for the popup, FooEdit.cshtml</p>
  
  <pre><code>@page
  @model Pages.FooEditModel
  
  @(Layout=null)
  
  &lt;form method="post"&gt;
      &lt;input asp-for="FooEditVM.Name" class="form-control" /&gt;&lt;br /&gt;
      &lt;input asp-for="FooEditVM.Stuff1" class="form-control" /&gt;&lt;br /&gt;
      &lt;input asp-for="FooEditVM.Stuff2" class="form-control" /&gt;&lt;br /&gt;
      &lt;input type="submit" value="Save"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And the code behind for the popup, FooEdit.cshtml.cs</p>
  
  <pre><code>public class FooEditModel : PageModel
  {
      [BindProperty]
      public FooEditVM FooEditVM { get; set; }
  
      public void OnGet(int id)
      {
          FooEditVM = new FooEditVM
          {
              Name = $"This is item {id}",
              Stuff1 = "Stuff1",
              Stuff2 = "Stuff2"
          };
      }
  
      public void OnPost(int id)
      {
          // How do we get to here???
          var a = FooEditVM.Name;
      }
  }
  
  public class FooEditVM
  {
      public string Name { get; set; }
      public string Stuff1 { get; set; }
      public string Stuff2 { get; set; }
  }
  </code></pre>
  
  <p>I've been through all the MS Tutorial stuff on Asp.net Core 2.2, but it doesn't seem to cover this.</p>
  
  <p>Also as a side question, although it works, is there a "ASP helper tag" way of doing the ajax bit?</p>