如何解决asp.net web API中更新用户的问题

时间:2018-04-24 09:59:39

标签: angularjs asp.net-web-api asp.net-identity

我为更新用户开发函数,但在后端,我有错误:

  

System.NullReferenceException:'对象引用未定义为对象的实例。'用户为空。

我认为因为用户为空,我不知道如何拨打电话来填充用户数据。这个错误显示在条件if(id!= users.ID),如何解决这个问题,这是我的代码:

[ResponseType(typeof(void))]
       // [HttpPut]
        [AcceptVerbs("OPTIONS")]
        public IHttpActionResult PutUsers(string id, Users users)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != users.Id)
            {
                return BadRequest();
            }

            db.Entry(users).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UsersExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

和这个user.controller:

$scope.userEdit = function () { 
                console.log('edit');
                var idsForEdit = [];
                angular.forEach($scope.listeOfUsers, function (item,$uibModalInstance, index) {
                    console.log($scope.listeOfUsers);
                    if (item.checked) {
                        console.log(item.checked);
                        console.log(item.Id);
                       //idsForEdit.push(item);
                        $scope.registration.Email=item.Email;
                        $scope.registration.Password=item.PasswordHash;
                        $scope.registration.Users_Role=item.Role;
                        $scope.registration.Site=item.Site;
                        $scope.registration.Id=item.Id;

                        $scope.ok = function () {
                            console.log("ok");
                           // $scope.Action = "Update";

                            User.Update({
                                    id: item.Id
                                }, $scope.Users=item.Users
                                , function (response) {
                                    console.log(response);
                                    console.log("ok");
                                    SweetAlert.swal({
                                        title: "Opération effectuée avec succès!",
                                        text: "Click pour quitter!",
                                        type: "success"
                                    });
                                    $state.go($state.current, $stateParams, {
                                        reload: true,
                                        inherit: false,
                                        notify: true
                                    });
                                    $uibModalInstance.close();
                                },
                                function (err) {
                                });

                            console.log($scope.user);

                        };
                    }
                });
                //$scope.isEditDirty==true;
            };

1 个答案:

答案 0 :(得分:0)

您的代码应为-

[ResponseType(typeof(void))]
       // [HttpPut]
        [AcceptVerbs("OPTIONS")]
        public IHttpActionResult PutUsers(string id, Users users)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if(users != null)
            {
              if (id != users.Id)
              {
                  return BadRequest();
              }

              db.Entry(users).State = EntityState.Modified;

              try
              {
                  db.SaveChanges();
              }
              catch (DbUpdateConcurrencyException)
              {
                  if (!UsersExists(id))
                  {
                      return NotFound();
                  }
                  else
                  {
                      throw;
                  }
              }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

使用C#6.0的新功能-空条件运算符

  [ResponseType(typeof(void))]
           // [HttpPut]
            [AcceptVerbs("OPTIONS")]
            public IHttpActionResult PutUsers(string id, Users users)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                if (id != users?.Id)
                {
                    return BadRequest();
                }

                db.Entry(users).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UsersExists(id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }


                return StatusCode(HttpStatusCode.NoContent);
            }