title: Populate a table from dropdown in asp.net mvc 5
body: <p>I am using asp.net mvc 5. I have created a the following basic application.</p>
  
  <p>I want to add items to a list from a drop down and show them in a html table. For a example i want to add multiple subjects to a student from a drop down and show them in a table under the drop down.
  I have created the following view and view model.</p>
  
  <pre><code>public class Subject
  {
      public string SubjectId {get; set;}
      public string SubjectName {get ; set;}
  }
  
  public class StudentViewModel
  {
      public string Name { get; set; }
      public string Grade { get; set; }
      public string Subject { get; set; }
  
      public List&lt;Subject&gt; subjectlist { get; set; }
  }
  
  
  
  
  @model School.Models.StudentViewModel
  
  &lt;form action="" method="post"&gt;
      &lt;p&gt;
          &lt;label&gt;Name&lt;/label&gt;
          @Html.TextBoxFor(model=&gt; model.Name)
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;Grade&lt;/label&gt;
          @Html.TextBoxFor(model=&gt; model.Grade)
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;Subject&lt;/label&gt;
          @Html.DropDownListFor(model =&gt; model.Subject, new SelectList(@ViewBag.Subjects, "value", "text"))
          &lt;button&gt;Add&lt;/button&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;table&gt;
              &lt;thead&gt;
                  &lt;tr&gt;
                      &lt;th&gt;Subject&lt;/th&gt;
                  &lt;/tr&gt;
              &lt;/thead&gt;
          &lt;/table&gt;
          &lt;tbody&gt;
              @if (Model.subjectlist != null)
              {
                  @foreach(var s in subjectlist)
                  {
                      &lt;tr&gt;
                          &lt;td&gt;s.SubjectName&lt;/td&gt;
                      &lt;/tr&gt;
                  }
  
              }
          &lt;/tbody&gt;
      &lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>user should be able to add multiple subjects which selected from drop down when he click the add button.but there is a issue. With my code when i click the add button it successfully add the selected subject to the list add show them in below table.But every time i add the subject it recreate the "subjectlist" and previously added item is not there. what is the problem in my code. someone please help.</p>
  
  <p>here is my controller code.</p>
  
  <pre><code> public ActionResult Index()
   {
      StudentViewModel model = new StudentViewModel();
      return View(model);
   }
  
   [HttpPost]
   public ActionResult Index(StudentViewModel svm){
      var sub = db.Subjects.Where(x=&gt; x.SubjectId == svm.Subject).FirstOrDefault();
      if(svm.subjectlist == null)
          svm.subjectlist = new List&lt;Subject&gt;();
      svm.subjectlist.Add(sub);
      return View("Index",svm);
   }
  </code></pre>