I'm having an issue posting data to my controller using angularjs.
The data looks good while debugging, right until it hits my controller and then the arguments being passed in are always null.
public class NewCompArgs { public string Name { get; set; } public string Description { get; set; } } //Tested with [HttpPost] here, and it didn't work // Tested using [FromBody]NewCompArgs args, and splitting // out into Name and Decription strings with [FromBody] public ActionResult AddNewComponent(NewCompArgs args) { Component NewComp = new Component { Name = args.Name, Description = args.Description }; _context.Add(NewComp); _context.SaveChanges(); return Json(NewComp); } NewCompArgs args is null every time.
Here is the service I use to hit my controller:
app.factory('Components', function ($http, $q) { var service = {}; service.AddNewComponent = function (args) { var deferred = $q.defer(); $http.post('/Components/AddNewComponent', { args: args }).then(function (response) { deferred.resolve(response.data); }); return deferred.promise; }; return service; }); Here is the angular controller function:
$scope.newComponent = function () { Components.AddNewComponent($scope.compArgs).then(function (response) { }); } Just an example of the data I'm getting during debugging
Angular Controller: ng controller
Angular Service: ng service
MVC Controller: MVC Controller
Any advice is greatly appreciated!
{ args: args }but the server is expecting an object which containsnameanddescription, notargs.PostManAPI client? is it working there properly?ActionResult- it's wrong. I think you are extendingControllerinstead ofApiController.