title: many-to-many relationship in mvc and EF
body: <p>I have created a blog site where users can <code>Post</code> a blog, in my model and database using Entity Framework there is a many-to-many relationship between <code>Posts</code> and <code>Tags</code>. </p>
  
  <p>In my database I also have <code>PostTagMap</code>, however I don't see this in my EF Model. Not sure if I need to. The <code>PostTagMap</code> just contains the unique identifier for both Post and Tag nothing else. </p>
  
  <blockquote>
    <p>(should be noted I created the database from the model)</p>
  </blockquote>
  
  <p>On my <code>Create Post</code> View (and controller) I want the users to have the option to create tags (along with there post) making sure that they are identified with one another. However I cant find a way to do this.</p>
  
  <p><strong>Post Model:</strong></p>
  
  <pre><code>public partial class Post
  {
      public Post()
      {
          this.Tags = new HashSet&lt;Tag&gt;();
      }
  
      public int Id { get; set; }
      public string BlogUserEmail { get; set; }
      public int CategoryId { get; set; }
      public string Title { get; set; }
      public string ShortDescription { get; set; }
  
      [AllowHtml]
      public string Description { get; set; }
      public string Meta { get; set; }
      public string UrlSlug { get; set; }
      public bool Published { get; set; }
      public System.DateTime PostedOn { get; set; }
      public Nullable&lt;System.DateTime&gt; Modified { get; set; }
  
      public virtual BlogUser BlogUser { get; set; }
      public virtual Category Category { get; set; }
      public virtual ICollection&lt;Tag&gt; Tags { get; set; }
  }
  
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  @using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;Post&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.HiddenFor(model =&gt; model.BlogUserEmail, User.Identity.Name)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.ValidationMessageFor(model =&gt; model.BlogUserEmail)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.CategoryId, "Category")
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.DropDownList("CategoryId", String.Empty)
              @Html.ValidationMessageFor(model =&gt; model.CategoryId)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Title)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Title)
              @Html.ValidationMessageFor(model =&gt; model.Title)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.ShortDescription)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.ShortDescription)
              @Html.ValidationMessageFor(model =&gt; model.ShortDescription)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.TextAreaFor(model =&gt; model.Description)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.ValidationMessageFor(model =&gt; model.Description)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Create" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>There is no way for me to use a <code>EditorFor</code> field in my <code>Create</code> View for tags. This is because the <code>Create</code> view is bound to <code>@Model MyBlogger.Post</code>.</p>
  
  <p>Tags only show up as a collection and as such if I say:</p>
  
  <pre><code>@Html.EditorFor(model =&gt; m.Tags) 
  </code></pre>
  
  <p>Then it shows blank in my view when rendered. </p>
  
  <p>I have tried a <code>foreach</code> however that did not work. For Instance trying this in my <code>Create</code> view:</p>
  
  <pre><code>@foreach (var item in Model.Tags)
  {
      @Html.EditorFor(model =&gt; item.Name)
      @Html.EditorFor(model =&gt; item.Description)
      @Html.EditorFor(model =&gt; item.UrlSlug)
  }
  </code></pre>
  
  <p>I get the error: <code>Object reference not set to an instance of an object.</code></p>