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!

6
  • possible duplicate of How to post an object to WebAPI. and there are many more answers here on SO. use the search function. Commented Jul 19, 2015 at 22:40
  • seems like you are reading jQuery ajax docs to use $http and not reading angular docs Commented Jul 19, 2015 at 22:40
  • For the one who voted to close as "too broad", it's not. Commented Jul 19, 2015 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. Commented Jul 20, 2015 at 14:53
  • $scope.data isn't a string. Commented Jul 22, 2015 at 8:41

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
Thanks for the edit! That worked perfectly. I appreciate the help! If I could I'd give you a +1.
Thanks. I have already voted your question up; someone was mean and had downvoted it before me.
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.

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.