2

I've tried to make a contact page using AngularJS and C# controller using Web Api.

I am working on it all the last week without find the correct answer that suits me.

I tried also post thru AJAX, JQUERY and angular.

Controller code:

    [HttpPost, AllowAnonymous]
    [Route("ContactUs")]
    public void Post(ContactUsProperties EmailDetails)
    {
        //Code for sending email to the website owner...
    }

Angular JS Code:

app.controller("ContactController", function ($scope, $http) {



$scope.EmailDetails = {
    Phone: null,
    Email: null,
    Name: null,
    Subject: null,
    Body: null
};

$scope.EmailDetailsArr = [
    $scope.Phone,
    $scope.Email,
    $scope.Name,
    $scope.Subject,
    $scope.Body
];

$scope.EmailDetails = {
    Phone: $scope.Phone,
    Email: $scope.Email,
    Name: $scope.Name,
    Subject: $scope.Subject,
    Body: $scope.Body
};
$scope.CreateEmail = function () {

    console.log($scope.EmailDetails);
    console.log(JSON.stringify($scope.EmailDetails));

    $http({
        method: 'POST',
        url: '/ContactUs',
        data: JSON.stringify($scope.EmailDetail)
    });
}

The routing is working, I know it cause the debugger stops me everytime I click on "Send" button. I tried it as an array, as JSON, as XML, as Everything and the object is still null!, I tried with [FROMBODY] still none.

I will be honest that if it POST require change of Web Api Config so I didn't change..

Please Guys Help Me Solve This Out, Explain Will Accept Happily. Thanks :)

5
  • try this => url : '/ContactUs/Post' Commented Oct 17, 2017 at 13:54
  • Can you tell us what it is you expect your code to do? Commented Oct 17, 2017 at 13:54
  • There is no need to stringify the data. The $http service will automatically do that. Commented Oct 17, 2017 at 14:03
  • imgprasad --> the route is /ContactUs .. it works cause in visual studio debugger stop the running.. Hintham --> i try to send object to controller with details about the contact (the body, subject, and private details to reply him by phone or email and then name) Commented Oct 17, 2017 at 18:23
  • georgeawg --> didn't know that Thanks! Commented Oct 17, 2017 at 18:28

2 Answers 2

1

You should use [FromBody] in order to got Parameter Binding.

When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter.

In this example, the content type is application/json.

public void Post([FromBody] ContactUsProperties EmailDetails)
Sign up to request clarification or add additional context in comments.

1 Comment

I read about it and its saying about getting simple parameter, I want to transfer object that contains few strings in it and not 1 string. regardless to what I have read, I tried that and it didn't work...
0
$http({
    method: 'POST',
    url: '/ContactUs',
    data: JSON.stringify($scope.EmailDetail) <-- you are missing an 's' at the end
});

1 Comment

The object is still null

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.