title: Why is POST action losing model values?
body: <p>I have a ViewModel which I'm trying to post back to save in the DB. While the ViewModel is getting the values set in the <code>//GET Create(int id)</code> it is losing some of them by the time it gets to the <code>//POST Create ()</code>. The <code>GoalLevel</code> and <code>GoalTitle</code> are being passed through OK, but the <code>ActorId</code> and <code>ProjectId</code> are being lost and even though the debugger is going through the <code>if (Model.State.IsValid)</code> block, the values that are going through aren't being saved to the DB. The redirect then falls over, because I've no longer got a valid <code>ProjectId</code> to pass to the URL. </p>
  
  <p>I'm sure it's something obvious if you know what you're doing!</p>
  
  <p><strong>CONTROLLER</strong></p>
  
  <pre><code>  // GET: UseCases/Create
  
      public ActionResult Create(int id)
  
      {
  
          ViewBag.projectId = id;
  
          //Populate the ViewModel
          UserGoalsStepViewModel model = new UserGoalsStepViewModel()
          {
              ProjectId = id,
              ActorList = new SelectList(db.Actors.Where(a =&gt; a.projectID == id), "id", "Title"),
  
          };
  
          return View(model);
      }
  
      // POST: UseCases/Create
      // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
      // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
      [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create([Bind(Include = "ProjectId,ActorId,GoalTitle,Level")] UserGoalsStepViewModel model)
      {
  
  
  
  
          if (ModelState.IsValid)
          {
  
              //Create a use case object to map to, which can then be saved in the DB
  
              UseCase uc = new UseCase();
              uc.ProjectID = model.ProjectId;
              uc.ActorID = model.ActorId;
              uc.Level = (Level)model.Level;
              db.SaveChanges();
              return RedirectToAction("Index", new { id = model.ProjectId });
          }
  
          return View(model);
      }           
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code> public class UserGoalsStepViewModel
      {
          public enum GoalLevel
          {
              Summary, UserGoal, SubGoal,
          }
  
              public int ProjectId { get; set; }
              public int ActorId { get; set; }
              [DisplayName("Actor Title")]    
              public SelectList ActorList { get; set; }
              [DisplayName("Goal Title")]
              public string GoalTitle { get; set; }
              public GoalLevel Level { get; set; }
  
              public UserGoalsStepViewModel ()
              {
  
              }
  
  
      }
  </code></pre>
  
  <p><strong>VIEW</strong></p>
  
  <pre><code>@using (Html.BeginForm()) 
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;UseCase&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.ActorList, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownListFor(model =&gt; model.ActorList, Model.ActorList, new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.ActorList, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.GoalTitle, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.GoalTitle, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.GoalTitle, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Level, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EnumDropDownListFor(model =&gt; model.Level, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.Level, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p><strong>UPDATED POST METHOD WHICH IS STILL GIVING AN ERROR AND NOT SAVING TO DB</strong></p>
  
  <p>After amends, I'm now getting this error <code>Message=The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.UseCase_dbo.Actor_ActorID". The conflict occurred in database "JustSpecIt", table "dbo.Actor", column 'ID'</code> when attempting to save to DB. </p>
  
  <p>Here is relevant bit of updated POST method: </p>
  
  <pre><code>if (ModelState.IsValid)
              {
  
                  //Create a use case object to map to, which can then be saved in the DB
  
                  UseCase uc = new UseCase();
                  uc.ProjectID = model.ProjectId;
                  uc.ActorID = model.ActorId;
                  uc.Title = model.GoalTitle;
                  uc.Level = (Level)model.Level;
                  db.UseCases.Add(uc);
                  db.SaveChanges();
                  return RedirectToAction("Index", new { id = model.ProjectId });
              }
  </code></pre>