如何解决ASP.NET Web API中更新用户的问题



i为更新用户开发功能,但是在后端,我有错误:

system.nullReferenceException:"对象引用未定义为对象的实例。"用户无效。

我认为,因为用户是无效的,并且我不知道如何打电话以填写用户数据。如果(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;
            };

您的代码应为 -

[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新功能-NULL条件操作员

  [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);
            }

相关内容

  • 没有找到相关文章

最新更新