title: How to insert data into many to many relationship tables asp.net mvc
body: <p>I am new to ASP.NET mvc and I have a doubt. How can I insert data into bridge table of many to many relationship table.</p>
  
  <p>I have two models like 
  class.cs and student.cs</p>
  
  <pre><code>public class Class
  {
  public int classId {get; set;}
  public string classname {get; set;}
  public virtual List&lt;Student&gt; Class {get; set;}
  }
  
  public class Student
  {
  public int studentId {get; set;}
  public string studentname {get; set;}
  public virtual List&lt;Class&gt; Class {get; set;}
  }
  
  //data layer
  public class ClassConfiguration : EntityTypeConfiguration&lt;Class&gt;
  {
  public ClassConfiguration()
  {
  HasMany(c =&gt; c.Student).WithMany(c =&gt; c.Class).Map(c =&gt; {c.MapLeftKey("classId");
  c.MapRightKey("studentId");
  c.ToTable("ClassStudentTable");
  });
  }
  }
  
  //I can see ClassStudentTable in database not in entities.
  
  //Mapping between these two
  public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
  public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
  public DbSet&lt;Class&gt; Class {get; set;}
  public DbSet&lt;Student&gt; Student {get; set;}
  
  protected override void OnModelCreating(DbModleBuilder modelBuilder)
  {
  modelBuilder.Configurations.Add(new ClassConfiguration());
  
  base.OnModelCreating(modelBuilder);
  }
  </code></pre>
  
  <p>I have two tables class and student with bridge table ClassStudentTable.
  Using default mvc scaffolding- Create method for Student</p>
  
  <pre><code>//http:get for student
  public ActionResult Create()
  {
  ViewBag.Class = new SelectList(db.Class, "classId", "classname");
  return View();
  }
  
  //http:post for student
  [HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Create(Bind(Include = "studentId,studentname") Student student)
  {
  if(ModelState.IsValid)
  db.Student.Add(student);
  db.SaveChanges();
  return RedirectToAction("Index");
  }
  </code></pre>
  
  <p>Now view for create method is</p>
  
  <pre><code>@model TestProject.Models.Student
  @{
  ViewBag.Title = "Create";
  }
  @using (Html.BeginForm())
  {
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  &lt;div class="form-horizontal"&gt;
   &lt;div class="form-group"&gt;
    @Html.LabelFor(model =&gt; model.studentname, htmlAttributes: new {@class = "control-label col-md-2"})
     &lt;div class="col-md-10"&gt;
      @Html.EditorFor(model =&gt; model.studentname, new { htmlAttributes = new {@class ="form-control"}})
      @Html.ValidateMessengerFor(model =&gt; model.studentname, "", new {@class = "text-danger"})
     &lt;/div&gt;
   &lt;/div&gt; 
  
  &lt;table class="table" id="tbClass"&gt;
   &lt;tr class="tr-header"&gt;
    &lt;th&gt;Class&lt;/th&gt;
    &lt;th&gt;&lt;a href="javascript:void(0);" id="addMore"&gt;&lt;/th&gt;
   &lt;/tr&gt;
  
   &lt;tr&gt;
    &lt;td&gt;
     &lt;div class="col-md-10"&gt;
      @Html.DropDownList("Class", null, htmlAttributes: new { @class = "control-label col-md-2" })
      @Html.ValidationMessageFor(model =&gt; model.Class, "", new { @class = "text-danger" })
     &lt;/div&gt;
    &lt;/td&gt;
    &lt;td&gt;&lt;a href='javascript:void(0);' class='remove'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;
   &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;div class="form-group"&gt;
   &lt;input type="submit" value="Create" class="btn btn-success"&gt;
   @Html.ActionLink("Cancel", "Index", null, new {@class = "btn btn-default"})
  &lt;/div&gt;
  &lt;/div&gt;
  }
  
  &lt;script&gt;
  $(function () {
      $('#addMore').on('click', function () {
          var data = $("#tbClass tr:eq(1)").clone(true).appendTo("#tbClass");
          data.find("input").val('');
      });
  
      $(document).on('click', '.remove', function () {
          var trIndex = $(this).closest("tr").index();
          if (trIndex &gt; 1) {
              $(this).closest("tr").remove();
          } else {
              alert("Sorry!! Should have atleast on row!");
          }
      });
  });
  </code></pre>
  
  <p></p>
  
  <p>this all the source code files I am having right now.</p>
  
  <p>Now my question is clear. </p>
  
  <p>when I add new dynamic rows into the table for adding new class to student(shows a dropdownlist for selecting a class for student). I can select as many classes as I can for a student on front BUT HOW CAN I PASS THE VALUES TO CREATE() [HTTPPOST]. </p>
  
  <p>can I pass through (List classs) as [parameters]?? if so how can I convert that table data into List.</p>
  
  <p>Waiting for reply</p>