1

I am trying to update a records in my project but I'm stuck and wondering if I could get some help.

Angular Controller

$scope.userId = 1;
$scope.Age = 23;

$scope.updateRec = function(){

 var post = myservice.update($scope.userId, $scope.Age);
 post.then(function(data))
  return data;
 });
}

myservice js

this.update(id, age){
   var request = $http({
                method: 'put',
                url: '/api/updateuser/update/' + id,
                data: age
            });
            return request;

}

Api Controller

private context _db = new context();

[AcceptVerbs("GET", "POST")]
[HttpGet]
public bool Update(UserTable model)
{
    model.age = //<< here is where I'm stuck, how do I update the user's age here ?

     _db.SaveChanges();
     return true;
}

model

public class UserTable{
  public int userId {get:set;}
  public stringName {get;set;}
  public int Age {get;set;}
}

As you can see I'm trying to update the user's age by using the userId. but I'm stuck on how I receive the age from the client and update the database. Could some direct me here please?

2
  • Added Entity Framework as that's where the actual question lies Commented Jun 6, 2016 at 11:07
  • You need to either tell your model that you've given it age, or change your parameters. ajax: data : { Age = age } or webapi: bool Update(int userId, int age). Then add the code to update your EF db. Commented Jun 6, 2016 at 11:09

3 Answers 3

1

You need to fix several things in your code to achieve what you want:

myservice.js

function Update(params) {
    return $http.put('/api/updateuser/update/', params);
}

Angular Controller

//User object
$scope.userModel = {
    userId: 1,
    Age: 23
};  

$scope.updateRec = function(){

    //Call service passing parameters
     var post = myservice.Update($scope.userModel).then(function (response){

        return response;

     }, function (response) {
        //do something with error message..
     })

}

API Controller C#

[HttpPut]
public bool Update(UserTable model)
{
    //Get the user by Id
    var user = _db.Users.SingleOrDefault(x => x.userId == model.userId);

    if(user == null)
        //Handle not existing user..

    //Change necessary Fields...
    user.Age = model.Age;

    //Commit changes
    _db.SaveChanges();

     return true;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You should get an instance of your user. If you are using ASP.NET identity you can get using identity.GetUserById or something like this. After that you set the new age to the object. _db.SaveChanges should do the rest.

Comments

0
  1. First find the user to update from the database by ID.

Pseudo code example:

var user = _db.Users.FirstOrDefault(i => i.UserId == model.userId);
  1. Now you can update the users age and then call SaveChanges() to persist the changes to the database.

Pseudo code example:

user.Age = model.age;
_db.SaveChanges();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.