title: Razor Pages: Values not staying from OnGet() to OnPost()
body: <p>I'm working on a ASP.NET Core webpage using Razor Pages. On one page, a customer is added or selected and the customer_id is passed through correctly in the OnGet function (seen through debugging). When I then have the user fill out the form, I try to make a call to a SQL server database using Entity Framework. Everything should be working correctly, however, in OnPostAsync(), when I try to import the customer_id value I had set in OnGet(), I get an error because the value somehow gets set to 0 instead of what I had it previously set as. I don't know why this is happening as I'm generally new to Razor Pages and Entity Framework, but I've posted the code below. Any suggestions would be great!</p>
  
  <p>AddAssessment.cshtml.cs:</p>
  
  <pre><code>namespace CustomerPageTest.Pages.Assessment
  {
  public class AddAssessmentModel : PageModel
  {
      private readonly CustomerPageTest.Data.CustomerPageTestContext _context;
  
      public AddAssessmentModel(CustomerPageTest.Data.CustomerPageTestContext context)
      {
          _context = context;
      }
  
      public static List&lt;SelectListItem&gt; InDatabaseUserData()
      {
          List&lt;SelectListItem&gt; Users = new List&lt;SelectListItem&gt;();
  
              string connString = "Data Source = DESKTOP-5A23I9M; Initial Catalog = DataWarehouse; User ID = sa; pwd = 2128Swan";
              string commandString = "SELECT user_id, name FROM KelderUser";
  
              SqlConnection conn = new SqlConnection(connString);
              SqlDataAdapter adapter = new SqlDataAdapter(commandString, conn);
              DataTable dtCustomers = new DataTable();
              adapter.Fill(dtCustomers);
  
              foreach (DataRow dataRow in dtCustomers.Rows)
              {
                  SelectListItem temp = new SelectListItem()
                  {
                      Value = dataRow[0].ToString(),
                      Text = dataRow[1].ToString()
                  };
                  Users.Add(temp);
              }
          return Users;
      }
  
      [BindProperty]
      public CustomerPageTest.Assessment Assessment { get; set; } = new CustomerPageTest.Assessment();
      public List&lt;SelectListItem&gt; SelectUser { get; set; } = new List&lt;SelectListItem&gt;();
  
      public void OnGet(int customerId)
      {
          SelectUser = InDatabaseUserData();
          Assessment.customer_id = customerId;
          if(Assessment == null)
          {
              return RedirectToPage("/Customer/List");
          }
          return Page();
      }
  
      // To protect from overposting attacks, enable the specific properties you want to bind to, for
      // more details, see https://aka.ms/RazorPagesCRUD.
      public async Task&lt;IActionResult&gt; OnPostAsync()
      {
          if (!ModelState.IsValid)
          {
              return Page();
          }
  
          foreach(var item in SelectUser)
          {
              if (item.Selected)
              {
                  try { Assessment.user_id = Convert.ToInt32(item.Value); } catch (Exception) { }
              }
          }
  
          try { Assessment.imported_data_datetime = Convert.ToDateTime(Assessment.imported_data_datetime); } catch(Exception) { }
          _context.Assessment.Add(Assessment);
          await _context.SaveChangesAsync();
  
          return RedirectToPage("/Customer/List");
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Then AddCustomer.cshtml:</p>
  
  <pre><code>@page
  @model CustomerPageTest.Pages.Assessment.AddAssessmentModel
  
  @{
      ViewData["Title"] = "AddAssessment";
  }
  
  &lt;h1&gt;AddAssessment&lt;/h1&gt;
  
  &lt;h4&gt;Assessment&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-4"&gt;
          &lt;form method="post"&gt;
              &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Assessment.vcenter" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="Assessment.vcenter" class="form-control" /&gt;
                  &lt;span asp-validation-for="Assessment.vcenter" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Assessment.imported_data_datetime" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="Assessment.imported_data_datetime" class="form-control" /&gt;
                  &lt;span asp-validation-for="Assessment.imported_data_datetime" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Assessment.notes" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="Assessment.notes" class="form-control" /&gt;
                  &lt;span asp-validation-for="Assessment.notes" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Assessment.user_id"&gt;&lt;/label&gt;
                  &lt;select class="form-control" asp-for="Assessment.user_id" asp-items="Model.SelectUser"&gt;
                      &lt;option value=""&gt;Select a User&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-primary" /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;a asp-page="/Customer/List"&gt;Back to List&lt;/a&gt;
  &lt;/div&gt;
  
  @section Scripts {
      @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
  }
  </code></pre>
  
  <p>Also, If you have any questions about what is going on in my code, let me know as there are other classes and pages before this. The main idea is that I have an Assessment class with an integer: customer_id, that needs to be set and it's not getting set.</p>