title: MVC 3 3 Models on a Form - Recipe, Ingredient, RecipeIngredient
body: <p>For the past couple weeks I have been researching everything I possibly can and have seemed to  hit a wall on figuring out how to create a form using multiple models and saving to a database.</p>
  
  <p>I am trying to be able to add 1 or more ingredients to a recipe on a single form create. I have created all the generated template crud views. I'm confused on which direction to go with this and am debating on looking into using MVC Controls Toolkit as well because I need the ability to add another ingredient on the form using jquery and ajax. Right now though, my main concern is just being able to bind the Recipe, Ingredient, and RecipeIngredient model if needed onto the form.</p>
  
  <p>Any help will be greatly appreciated with setting up my view and controller methods! Thanks in advance :)</p>
  
  <p>Here are my model classes, a viewmodel, the controller method.
  Unfortuantly I dont have enough reputation to post a image but here is the link to my database diagram. <a href="http://i44.tinypic.com/xp1tog.jpg" rel="nofollow">http://i44.tinypic.com/xp1tog.jpg</a>.
  I hope this is enough detail but if more is needed please let me know.</p>
  
  <pre><code>    public ActionResult CreateFullRecipe()
      {
          var recipeViewModel = new RecipeViewModel();
          //Im getting a RecipeIngredients null error as well below..
          recipeViewModel.RecipeIngredients.Add(new RecipeIngredient());
          return View(recipeViewModel);
      }
  
  public class Recipe
  {
      public int RecipeID { get; set; }
      public string RecipeName { get; set; }
      public string Description { get; set; }
      public int? PrepTime { get; set; }
      public int? CookTime { get; set; }
      public string ImageURL { get; set; }
  
      public virtual ICollection&lt;RecipeTag&gt; RecipeTags { get; set; }
      public virtual ICollection&lt;Rating&gt; Ratings { get; set; }
      public virtual ICollection&lt;RecipeStep&gt; RecipeSteps { get; set; }
      public virtual ICollection&lt;RecipeIngredient&gt; RecipeIngredients { get; set; }
  
  }
  
  public class Ingredient
  {
  
      public int IngredientID { get; set; }
      public string IngredientName { get; set; }
  
      public virtual ICollection&lt;RecipeIngredient&gt; RecipeIngredients { get; set; }
  }
  
      public class RecipeIngredient
  {
      public int RecipeIngredientID { get; set; }
      public string IngredientDesc { get; set; }
      public string Amount { get; set; }
      public int RecipeID { get; set; }
      public int? IngredientID { get; set; }
  
      public virtual Recipe Recipe { get; set; }
      public virtual Ingredient Ingredient { get; set; }
  }
  
  public class RecipeViewModel
  {
      public Recipe Recipe { get; set; }
      public Ingredient Ingredient { get; set; }
      public ICollection&lt;RecipeIngredient&gt; RecipeIngredients { get; set; }
  }
  
  @using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;
      &lt;legend&gt;Recipe&lt;/legend&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Recipe.RecipeName)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Recipe.RecipeName)
          @Html.ValidationMessageFor(model =&gt; model.Recipe.RecipeName)
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Ingredient.IngredientName)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Ingredient.IngredientName)
          @Html.ValidationMessageFor(model =&gt; model.Ingredient.IngredientName)
      &lt;/div&gt;
  
  
  
      &lt;ul&gt;
      @foreach (var recipeingredient in Model.RecipeIngredients)
      {
          &lt;li&gt;
              @recipeingredient.Ingredient.IngredientName
         &lt;/li&gt;
      }
      &lt;/ul&gt;
  </code></pre>