title: MVC - pass all ListBox items back to Controller
body: <p>I created a database with the Entity Framework with the model first approach. Among other things, I have a type <code>ContentEntry</code> and a type <code>Tag</code>. Every <code>ContentEntry</code> can have multiple <code>Tag</code>s and every <code>Tag</code> can be used by multiple <code>ContentEntry</code>’s. It should be such that no tag exists twice in the db, for that is the n:m relation:
  <img src="https://i.stack.imgur.com/dOMp6.png" alt="Image of the realtion between Tag and ContentEntry"></p>
  
  <p>Now, I try to create a controller/view to create a new <code>ContentEntry</code> with <code>Tag</code>s. I have no idea how to create a ListBox which give <strong>all</strong> his items back to the controller. The JavaScript (with jQuery) is no problem for me: </p>
  
  <pre><code>&lt;span class="label"&gt;Tags&lt;/span&gt;
  @Html.ListBoxFor(model =&gt; model.Tags, 
                   new MultiSelectList(Model.Tags),
                   new { id = "lbTags" })
  
  &lt;input type="text" id="tbTag" /&gt;
  &lt;input type="button" value="add" onclick="addTag();" /&gt;
  &lt;input type="button" value="delete" onclick="delTags();" /&gt;
  
  &lt;script&gt;
      function addTag() {
          $('#lbTags').append($('&lt;option&gt;',
              { value: $("#tbTag").val(), text: $("#tbTag").val() }));
      }
      function delTags() {
          $("#lbTags option:selected").remove();
      }
  &lt;/script&gt;
  @Html.ValidationMessageFor(model =&gt; model.Tags, "", new { @class = "input-error" })
  </code></pre>
  
  <p>But my <code>Tags</code> collection stays always empty:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult CreateBlogEntry
    ([Bind(Include = "Id,Published,Title,AccountId,HtmlContent,Tags")]BlogEntry blogEntry)
  {
      //blogEntry.Tags is always empty
      if (ModelState.IsValid
      {
          db.ContentEntrySet.Add(blogEntry);
          db.SaveChanges();
          return RedirectToAction("Index");
      }
  </code></pre>
  
  <p><code>BlogEntry</code> is a derivate of <code>ContentEntry</code> and <code>ContentEntry.Tags</code> is a <code>ICollection&lt;Tag&gt;</code>.</p>
  
  <p>Has anyone an idea how to solve this task?</p>
  
  <p><strong>EDIT</strong>:
  Here is my GET-method for CreateBlogEntry:</p>
  
  <pre><code>public ActionResult CreateBlogEntry()
  {
      //ViewBag.Tags = db.TagSet;
      return View(new BlogEntry()
      {
          Published = DateTime.Now,
      });
  }
  </code></pre>