title: Session Data to Model MVC 5
body: <p>I want to use C# Sessions as a way to store data(potentially editing) instead of going back to the database repeatedly, especially with repopulating lists, plus this may go across a few pages. </p>
  
  <p>In ASP Web Forms I would do the following.</p>
  
  <p>Code behind:</p>
  
  <pre><code>if(!String.IsNullOrEmpty(Session["PersonSession"] as String)
  {
    Person newPerson = new Person();
  }
  else
  {
    Person newPerson = (Person)Session["PersonSession"];
    myTextBox.Text = newPerson.Name.ToString();
  }
  // submitting the data chucking the data the other way
  // then stored proc
  newPerson.Name = myTextBox.Text;
  </code></pre>
  
  <p>Using the above I was able to populate existing textboxes and submit the data back to the session upon a submit button click.</p>
  
  <p>How do I achieve this in Razor MVC 5?</p>
  
  <p><strong>Update</strong>
  I've been trying to utilise the suggested methods in the answers below and update as I go.
  Trying to store everything in session and let it display through ViewBag before everything gets dumped to the db.</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>public ActionResult Index()
          {
              if (!string.IsNullOrEmpty((string)Session["SessionObj"]))
              {
                  List&lt;Person&gt; People = new List&lt;Person&gt;();
              }
              else
              {
                  List&lt;Person&gt; People = (List&lt;Person&gt;)Session["SessionObj"];
  
                  //foreach(var p in People)
                  //{
                  //    p.Name.ToString();
                  //}
                  ViewBag.PersonList = People;
              }
              return View();
          }
  
          public ActionResult AddPerson([Bind(Include ="Name")]Person person)
          {
              // add session/viewbag to list of session/viewbag
              List&lt;Person&gt; People = (List&lt;Person&gt;)Session["SessionObj"];
              People.Add(person);
              Session["SessionObj"] = People;
  
              return View();
          }
  </code></pre>
  
  <p><strong>Index</strong></p>
  
  <pre><code>@model SessionData.Models.Person
  @{
      ViewBag.Title = "Index";
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  @if (ViewBag.PersonList != null)
  {
      foreach (var person in @ViewBag.PersonList)
      {
          &lt;div&gt;@person.Name&lt;/div&gt;
      }
  }
  
  @using (Html.BeginForm())
  {
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Employee&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Name, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Name, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="AddPerson" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  </code></pre>