title: ajax POST not passing back correct data
body: <p>I am trying to pass back a json object back to my action in my device controller, this is then inserted into the database, however when I click the submit button on my form it seems to fire twice.  Another problem is that my location field within the json object and pfID does not get sent back to the controller, the other fields get sent back properly.  Here is my code:</p>
  
  <pre><code>$('#getDevice').unbind('click').bind('click', function (e) {
      e.stopPropagation();
      //anti forgery token
      //get the form
      var form = $('#addDeviceForm');
      //from the form get the antiforgerytoken
      var token = $('input[name="__RequestVerificationToken"]', form).val();
  
      var URL = 'Devices/PushIPForCollection';
  
      //Before this we need to build the model from the input the user has given us
      var device = {
          deviceID: ' ',
          ipAddress: $('#dIPAddress').val(),
          deviceName: $('#dDeviceName').val(),
          CreatedDate: ' ',
          UpdatedDate: ' ',
          CreatedBy: ' ',
          UpdatedBy: ' ',
          deviceSubGroupID: $('#dSubgroup option:selected').val(),
          subgroup: " ",
          companyID: ' ',
          hidden: ' ',
          pfID: $('#dpfID option:selected').val(),
          pf: ' ',
          location: JSON.stringify({
              'region': $('#dRegion').val() == null ? ' ' : $('#dRegion').val(),
              'country': $('#dCountry').val() == null ? ' ' : $('#dCountry').val(),
              'city': $('#dCity').val() == null ? ' ' : $('#dCity').val(),
              'building': $('#dBuilding').val() == null ? ' ' : $('#dBuilding').val(),
              'room': $('#dRoom').val() == null ? ' ' : $('#dRoom').val(),
              'rack': $('#dRack').val() == null ? ' ' : $('#dRack').val()
          })
      };
  
      alert(device.pfID);
      alert(device.location);
  
      $.ajax({
          url: URL,
          data: {
              __RequestVerificationToken: token,
              device: device
          },
          type: 'POST',
          success: function (result) {
          },
          error: function (jqXHR, textStatus, errorThrown) {
              alert("An error has occurred please contact us");
          }
      })
      $('#add-device').modal('hide');
      return false;
  });
  </code></pre>
  
  <p>Where I alert my pfID it returns a string of <code>"ba4475ef-0eed-441a-a77e-d733c288bf8e"</code></p>
  
  <p>Here is my model:</p>
  
  <pre><code>public class Devices
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int deviceID { get; set; }
  
      [Display(Name="IP Address:"), StringLength(50)]
      public string ipAddress { get; set; }
  
      [Display(Name = "DeviceName:"), StringLength(50)]
      public string deviceName { get; set; }
  
      public DateTime? CreatedDate { get; set; }
      public DateTime? UpdatedDate { get; set; }
      public string CreatedBy { get; set; }
      public string UpdatedBy { get; set; }
  
      [Display(Name="Add to subgroup:")]
      public long? deviceSubGroupID { get; set; }
  
      [ForeignKey("deviceSubGroupID")]
      public DeviceSubGroup subgroup { get; set; }
      public string companyID { get; set; }
      public string hidden { get; set; }
      public string pfID { get; set; }
      [ForeignKey("pfID")]
      public PfHealth.Pf pf { get; set; }
      public string location { get; set; }
  }
  </code></pre>
  
  <p>and here is my post method:</p>
  
  <pre><code> public ActionResult PushIPForCollection(Devices device)
      {
          //Before committing to the database we need to check if the device already exists
          var checkIfDeviceExists = db.devices.Any(check =&gt; check.ipAddress == device.ipAddress);
  
          if (!checkIfDeviceExists)
          {
              if (ModelState.IsValid)
              {
                  var userID = User.Identity.GetUserId();
  
  
                  device.CreatedDate = DateTime.Now;
                  device.UpdatedDate = DateTime.Now;
                  device.CreatedBy = userID;
                  device.UpdatedBy = userID;
  
                  var subgroup = db.deviceSubGroups.Where(sub =&gt; sub.deviceSubID == device.deviceSubGroupID).FirstOrDefault();
                  device.communityString = subgroup.snmpCommunityString;
                  device.companyID = subgroup.companyID;
  
                  db.devices.Add(device);
                  db.SaveChanges();
              }
  
          }
          //Can change this to get json in order to let to view know what message to display to the user.
          return RedirectToAction("index");
      }
  </code></pre>
  
  <p>and here is my form in the view:</p>
  
  <pre><code>&lt;div class="modal fade" id="add-device" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt;
  &lt;div class="modal-dialog"&gt;
      &lt;div class="modal-content"&gt;
          &lt;div class="modal-header"&gt;
              &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
              &lt;h4 class="modal-title" id="add-device-Label"&gt;&lt;strong&gt;ADD DEVICE&lt;/strong&gt;&lt;/h4&gt;&lt;!--add depending on which panel you have clicked--&gt;
          &lt;/div&gt;
          &lt;div class="modal-body" id="add-device-body"&gt;
              &lt;!--Depending on which panel insert content--&gt;
              @using (Html.BeginForm("PushIPForCollection", "Devices", FormMethod.Post, new { id = "addDeviceForm" }))
              {
                  @Html.AntiForgeryToken()
  
                  &lt;hr /&gt;
                  &lt;div class="form-horizontal"&gt;
                      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.LabelFor(m =&gt; m.device.ipAddress, new { @class = "col-md-2 control-label"})
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.TextBoxFor(m =&gt; m.device.ipAddress, new { @class = "form-control", @id = "dIPAddress" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.LabelFor(m =&gt; m.device.deviceName, new { @class = "col-md-2 control-label" })
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.TextBoxFor(m =&gt; m.device.deviceName, new { @class = "form-control", @id = "dDeviceName" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.LabelFor(m =&gt; m.device.deviceSubGroupID, new { @class = "col-md-2 control-label" })
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.DropDownListFor(m =&gt; m.device.deviceSubGroupID, (IEnumerable&lt;SelectListItem&gt;)ViewBag.subGroups, "None", new { @id = "dSubgroup" })
                              @Html.ValidationMessageFor(m =&gt; m.device.deviceSubGroupID, "", new { @class = "text-danger" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.Label("Region:")
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.TextBox("Region", "", new { @class = "form-control", @id = "dRegion" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.Label("Country:")
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.TextBox("Country", "", new { @class = "form-control", @id = "dCountry" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.Label("City:")
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.TextBox("City", "", new { @class = "form-control", @id = "dCity" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.Label("Building:")
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.TextBox("Building", "", new { @class = "form-control", @id = "dBuilding" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.Label("Room:")
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.TextBox("Room", "", new { @class = "form-control", @id = "dRoom" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.Label("Rack:")
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.TextBox("Rack", "", new { @class = "form-control", @id = "dRack" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-3"&gt;
                              @Html.LabelFor(model=&gt;model.device.pfID)
                          &lt;/div&gt;
                          &lt;div class="col-md-9"&gt;
                              @Html.DropDownListFor(m =&gt; m.device.pfID, (IEnumerable&lt;SelectListItem&gt;)ViewBag.pathfinders, "None", new { @id = "dpfID" })
                              @Html.ValidationMessageFor(m =&gt; m.device.pfID, "", new { @class = "text-danger" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="modal-footer"&gt;
                              &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                              &lt;button id="getDevice" type="submit" value="CreateGroups" data-loading-text="Loading..." class="btn btn-primary"&gt;Save changes&lt;/button&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
              }
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>