title: List.count always resetting to 0 after adding element
body: <p>I am creating a Web Form using visual Studio 2010 and I have a .aspx page that grabs some user input and saves it to a database. There are two buttons on the page, one for the user to "save" values (should be added to the list here) and the second to actually save them to the database in the proper format. My code is below:</p>
  
  <pre><code>namespace TabletApplication.HomePages
  {
      public partial class Record : System.Web.UI.Page
      {
          List&lt;ReadingModel&gt; paramList;
  
          protected void Page_Load(object sender, EventArgs e)
          {
              paramList = new List&lt;ReadingModel&gt;();
          }
  
  
  
          public class ReadingModel
          {
              public int paramId { get; set; }
              public string value { get; set; }
              public Boolean pass { get; set; }
              public string comment { get; set; }
          }
  
          protected void btnUpdate_Click(object sender, EventArgs e)
          {
              // Get an instance of the Button
              Button UpdateButton = (Button)sender;
              // Get the ID of the current record from the CommandArgument
              int intID = Convert.ToInt32(UpdateButton.CommandArgument);
              bool goodValue;
              using (TabletApplicationEntities serverContext = new TabletApplicationEntities())
              {
                  // Get the value and comment
                  TextBox Reading =
                      (TextBox)UpdateButton.Parent.FindControl("txtReading");
                  TextBox Comment =
                      (TextBox)UpdateButton.Parent.FindControl("txtComment");
  
                  //Get min and max values
                  Label Min = (Label)UpdateButton.Parent.FindControl("minValLabel");
                  Label Max = (Label)UpdateButton.Parent.FindControl("maxValLabel");
                  double minVal = Convert.ToDouble(Min.Text);
                  double maxVal = Convert.ToDouble(Max.Text);
                  int readingVal = -1;
  
                  try
                  {
                      readingVal = Convert.ToInt32(Reading.Text);
                      goodValue = true;
                  }
                  catch
                  {
                      Reading.Text = "Invalid entry: Must enter a number";
                      Reading.BackColor = System.Drawing.Color.Red;
                      Reading.ForeColor = System.Drawing.Color.Blue;
                      goodValue = false;
                  }
  
                  if (goodValue)
                  {
                      // Update the record
                      ReadingModel Result = new ReadingModel();
                      Result.comment = Comment.Text;
                      Result.value = Reading.Text;
                      Result.paramId = intID;
                      if (readingVal &gt; minVal &amp;&amp; readingVal &lt; maxVal &amp;&amp; readingVal &gt;= 0)
                      {
                          Result.pass = true;
                      }
                      else if (readingVal &lt; minVal || readingVal &gt; maxVal)
                      {
                          Result.pass = false;
                      }
                      else
                      {
                          Reading.Text = "UNKNOWN ERROR: Try again";
                          Reading.BackColor = System.Drawing.Color.Red;
                          Reading.ForeColor = System.Drawing.Color.Blue;
                      }
                      paramList.Count();
                      paramList.Add(Result);
                      paramList.Count();
                  }
              }
          }
  
          protected void btnSubmit_Click(object sender, EventArgs e)
          {
              using (TabletApplicationEntities serverContext = new TabletApplicationEntities())
              {
                  // Create a new Reading
                  foreach (var para in paramList)
                  {
                      Reading objReading = new Reading();
                      // Set values
                      objReading.ParamID = para.paramId;
                      objReading.comment = para.comment;
                      objReading.date = System.DateTime.Now;
                      objReading.pass = para.pass;
                      objReading.reading1 = para.value;
  
                      // Add object
                      serverContext.Readings.AddObject(objReading);
                  }
  
                  // Save changes
                  serverContext.SaveChanges();
                  paramList.Clear();
              }
          }
      }
  }
  </code></pre>
  
  <p>When the user hits the first button, the <code>btnUpdate_Click</code> function runs properly, and adds the new <code>Result</code> to <code>paramList</code>. With breakpoints on the two <code>paramList.Count()</code> I can see that the count is 0 before the result is added, and 1 afterwards, like it should be. When the save button is pressed, however, the <code>btnSubmit_Click</code> function runs and completely skips over the for loop because the count of <code>paramList</code> is 0 again. Any Ideas?</p>
  
  <p>EDIT 1:</p>
  
  <p>I tried adding a ViewState to the Page_Load, but I believe it's not working because List is not "serializeable" but i don't know how to fix it. This is what I added:</p>
  
  <pre><code>List&lt;ReadingModel&gt; paramList;
  
          protected void Page_Load(object sender, EventArgs e)
          {
              if (ViewState["paramList"] != null)
              {
                  paramList = (List&lt;ReadingModel&gt;)ViewState["paramList"];
              }
              else
              {
                  paramList = new List&lt;ReadingModel&gt;();
              }
          }
  </code></pre>