So I have a Web API function that looks like this
[Route("api/Account/Save")]
[HttpPut]
public IHttpActionResult SaveAccount(Account acc)
{
if (acc.AccountID != null)
{
// do stuff
var result = new {};
}
return Ok(result);
}
When I go to that api/Account/Save endpoint, I need to save an instance of the Account class to the database. The UI of the web page should have the user entered stuff like ID, Name, ...
I am having trouble passing the instance of the class to the method. I know how to pass something like a string through the endpoint then to the function, but I don't know how to do this.
This is my AngularJS. I put these in separate JS files that's why I am declaring things multiple times.
angular.module('MyApp')
.factory('accountResource', function ($resource) {
return {
saveAccount: $resource('/api/Account/Save', {}, {
query: {
method: 'PUT',
params: {
// probably pass it here?
// I pass stuff like ID's to the function here
}
}
})
};
});
angular.module('MyApp')
.factory('accountFactory', ['$q', 'accountResource', function ($q, accountResource) {
var service = {
saveAccount: saveAccount,
account: {}
}
function saveAccount(account) {
return accountResource.saveAccount.query().$promise.then(
function (result) {
service.account = result.account;
return result.$promise;
}, function (error) {
return $q.reject(error);
});
}
return service;
}]);
When I run the debugger and break at this line in acccountFactory
return accountResource.saveAccount.query().$promise.then(
The parameter account does have all the attributes (ID, account, ...), and I do see the values entered by the user are assigned to them. I just don't know how to pass this to the API function. And if I change my $resource like this, and pass account to query, it does not work (it only works when I'm passing stuff like ID's). Because I am passing in an object, the URL looks like this:
/api/Account/Save/%5Bobject%20Object%5D
angular.module('MyApp')
.factory('accountResource', function ($resource) {
return {
saveAccount: $resource('/api/Account/Save/:acc', {}, {
query: {
method: 'PUT',
params: {
acc: '@acc'
}
}
})
};
});