How to fix problem of update users in asp.net web

2019-08-22 23:31发布

I develop function for update user, but in backend, I have error:

System.NullReferenceException: 'The object reference is not defined to an instance of an object.' users was null.

I think because users is null, and I don't know how to make a call to fill users data. this error displays in the condition if(id!= users.ID) ,how to fix this problem, here is my code:

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

and this 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条回答
做自己的国王
2楼-- · 2019-08-23 00:03

Your code should be -

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

Using C# 6.0 new features - Null Conditional Operator

  [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);
            }
查看更多
登录 后发表回答