title: MVC EF - How to save Array to db
body: <p>I need to save an array of tags which I pass from view to controller using angular and json. I tried the following but vm.Name comes up as null.</p>
  
  <p><strong>script post request</strong></p>
  
  <pre><code>$http({
  method: 'POST',
  url: '/RecipeTags/CreateRecipeTag',
  data: $scope.selectedTags
  }).then(function (response) {
  }, function () { alert('Error CreateRecipeTag'); });
  </code></pre>
  
  <p><strong>sample array</strong></p>
  
  <pre><code>[{"Id":12,"Name":"Baking","RecipeId":0},{"Id":16,"Name":"Oriental","RecipeId":0},{"Id":17,"Name":"Japanese","RecipeId":0}]
  </code></pre>
  
  <p><strong>RecipeTagsController.cs</strong></p>
  
  <pre><code>[HttpPost]
      public JsonResult CreateRecipeTag(List&lt;RecipeTag&gt; vm)
      {
  
          foreach (var tag in vm)
          {
              RecipeTag recipeTag = new RecipeTag();
              recipeTag.Name = vm.Name;
              recipeTag.RecipeId = vm.RecipeId;
              db.RecipeTags.Add(recipeTag);
          }
          db.SaveChanges();
          return Json(vm, JsonRequestBehavior.AllowGet);
      }
  </code></pre>
  
  <p><strong>RecipeTag.cs</strong></p>
  
  <pre><code>public class RecipeTag
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      [Display(Name = "Tag")]
      public string Name { get; set; }
  
      [ForeignKey("Recipe")]
      public int RecipeId { get; set; }
      public virtual Recipe Recipe { get; set; }
  }
  </code></pre>