1

I've tried researching this but I can't find anything anywhere that's helpful or any relevant answers.

For my angular controller I have:

app.controller('AdminCtrl', function ($scope, $http) {
$scope.data = {
    Name: '',
    Password: ''
},
$scope.login = function (data) {
    $http({
        method: 'POST',
        url: '/login/postlogin',
        data: JSON.stringify({
            data: $scope.data
        }),
        contentType: "application/json",
        dataType: "json"
    }).success(function (data) {
        alert('success!');
    }).error(function (error) {
        alert(error.message);
    })
}
});

For my c# controller I have a very basic setup:

    [HttpPost]
    public string PostLogin(string data) {          
        return string.Empty;
    }

My issue is that inside of the c# controller, data is always null. Does anyone know how to pass the name and password params over to the c# controller? I know for a fact that I am recieving values for them from my textboxes that I have. I am also recieving the success message/alert.Thanks!

lightningfire
  • 117
  • 3
  • 9
  • possible duplicate of [How to post an object to WebAPI](http://stackoverflow.com/questions/27869763/how-to-post-an-object-to-webapi). and there are many more answers here on SO. use the search function. – Nasreddine Jul 19 '15 at 22:40
  • seems like you are reading jQuery ajax docs to use `$http` and not reading angular docs – charlietfl Jul 19 '15 at 22:40
  • For the one who voted to close as "too broad", it's not. – Nasreddine Jul 19 '15 at 22:40
  • @Nasreddine I took a look at that document before I posted here but it didn't really help me with what I am trying to do. After taking a look at the posted answers along with the other question I am getting closer to what I am trying to accomplish though. – lightningfire Jul 20 '15 at 14:53
  • `$scope.data` isn't a `string`. – Amit Kumar Ghosh Jul 22 '15 at 08:41
  • @AmitKumarGhosh, actually, if you take a look at HaukurHaf's post, you can pass them in as strings. – lightningfire Jul 22 '15 at 13:36

2 Answers2

3

Data is not a string. In c# make an object with Name and Password as properties and have this:

[HttpPost]
public string PostLogin(Data data) {  

Edit in response to comment: Yes you need to include the namespace that Data is in in your controller. You could try putting the Data object directly above.

 public class Data
    {
        public string Name { get; set; }
        public string Password { get; set; }
    }

For clarity, data should not be stringified, instead just sent as data: data

trees_are_great
  • 3,881
  • 3
  • 31
  • 62
  • I am trying to use the (Data data) but it keeps telling me that Data could not be found. Is there a namespace that I'm missing? I have successfully gotten it to work with the solution HaudurHaf posted, but I am wanting to pass it as an object since there could potentially be several params that I could be passing in here. When I use the generic (Object data) nothing comes through. – lightningfire Jul 20 '15 at 14:57
  • Glad you have it working, but yes good idea to pass objects. Hopefully that edit helps. You won't want to stringify it - will edit answer to make this clear in a bit. – trees_are_great Jul 20 '15 at 19:40
  • Thanks for the edit! That worked perfectly. I appreciate the help! If I could I'd give you a +1. – lightningfire Jul 21 '15 at 13:28
  • Thanks. I have already voted your question up; someone was mean and had downvoted it before me. – trees_are_great Jul 22 '15 at 08:18
1

Just send $scope.data directly, dont stringify it. Then modify your PostLogin method so it accepts two strings, one called Name and one called Password.

HaukurHaf
  • 13,522
  • 5
  • 44
  • 59