title: MVC 3 set default value on many to many relationship (iCollection)
body: <p>I'm learning MVC 3 (code first) right now, and I've learned a lot thanks to some great tutorials on asp.net and some questions here on stackoverflow, but I can't figure something out. I'm trying to set a default value. When someone askes a question, the status of that question has to be 3 (= unanswered) by default. I've manage to set that one up thanks to this post: <a href="https://stackoverflow.com/questions/7637022/default-value-in-an-asp-net-mvc-view-model">Default value in an asp.net mvc view model</a>
  but now I'm trying to do the same thing for a many on many relationship. (in the QuestionModel public virtual ICollection Categories)</p>
  
  <p>Can someone help me out on this one.</p>
  
  <p>This is my QuestionModel (Question.cs)</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  
  namespace MvcDatabase.Models
  {
      public class Question
      {
          [Key]
          public int QuestionID { get; set; }
  
          [DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
          [Display(Name="Datum")]
          public DateTime Date { get; set; }
  
          [Required(ErrorMessage="Naam is een verplicht veld.")]
          [MaxLength(100, ErrorMessage="Naam kan maximum 100 karakters bevatten")]
          [Display(Name="Naam")]
          public string Author { get; set; }
  
          [Required(ErrorMessage="E-mail is een verplicht veld.")]
          [DataType(DataType.EmailAddress)]
          [Display(Name = "Email")]
          public string MailAuthor { get; set; }
  
          [Required(ErrorMessage="Onderwerp is een verplicht veld.")]
          [MaxLength(100, ErrorMessage="Onderwerp kan maximum 100 karakters bevatten.")]
          [Display(Name="Onderwerp")]
          public string Title { get; set; }
  
          [Required(ErrorMessage="Bericht is een verplicht veld.")]
          [Display(Name="Bericht")]
          public string Message { get; set; }
  
          public virtual ICollection&lt;Category&gt; Categories { get; set; }
          public virtual ICollection&lt;Reaction&gt; Reactions { get; set; }
  
          [ForeignKey("Status")]
          public int StatusID { get; set; }
          public virtual Status Status { get; set; }
  
          public Question()
          {
              StatusID = 3;
          }
      }
  }
  </code></pre>
  
  <p>This is the CategoryModel (Category.cs)</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  
  namespace MvcDatabase.Models
  {
      public class Category
      {
          [Key]
          public int CategoryID { get; set; }
  
          [Required(ErrorMessage="Omschrijving is een verplicht veld.")]
          [MaxLength(100, ErrorMessage = "Omschrijving kan maximum 100 karakters bevatten.")]
          [Display(Name="Categorie omschrijving")]
          public string Description { get; set; }
  
          public virtual ICollection&lt;Question&gt; Questions { get; set; }
      }
  }
  </code></pre>
  
  <p>And this is the DbContextModel (TestotheekDBContext.cs)</p>
  
  <pre><code>using System.Data.Entity;
  
  namespace MvcDatabase.Models
  {
      public class TestotheekDBContext: DbContext
      {
          public DbSet&lt;Status&gt; Statuses { get; set; }
          public DbSet&lt;Category&gt; Categories { get; set; }
          public DbSet&lt;Question&gt; Questions { get; set; }
          public DbSet&lt;Employee&gt; Employees { get; set; }
          public DbSet&lt;Reaction&gt; Reactions { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;Question&gt;()
                  .HasMany(qc =&gt; qc.Categories)
                  .WithMany(cq =&gt; cq.Questions)
                  .Map(m =&gt; m.MapLeftKey("QuestionID")
                      .MapRightKey("CategoryID")
                      .ToTable("QuestionsWithCategory"));
          }
      }
  }
  </code></pre>
  
  <p>In case you need the code in the Controller</p>
  
  <pre><code>        public ActionResult Create()
          {
              return View(new Question());
          } 
  
          //
          // POST: /Default4/Create
  
          [HttpPost]
          public ActionResult Create(Question question)
          {
              var vraag = new Question();
              vraag.Date = DateTime.Now;
              vraag.Author = question.Author;
              vraag.MailAuthor = question.MailAuthor;
              vraag.Title = question.Title;
              vraag.Message = question.Message;
  
              if(ModelState.IsValid)
              {
                  db.Questions.Add(vraag);
                  db.SaveChanges();
                  return RedirectToAction("Index","Home");
              }
              return View(question);
          }
  </code></pre>
  
  <p>(Haven't tested the return RedirectToAction)</p>
  
  <p>EDIT</p>
  
  <p>This is the result I'm currently getting:
  <img src="https://i.stack.imgur.com/A2zYb.jpg" alt="enter image description here">
  As you can see in the last question in the first section (first table) there is no category (field is empty) this has to be the same as all the other questions in that table no category, since the admin has to assign one (or more).</p>