1

I'm creating a js object with the same properties as my controller action expects as parameters.

controller.js

(function () {
    'use strict';

    angular
        .module('app')
        .controller('requestController', requestController);

    requestController.$inject = ['$scope', 'lecturesFactory', 'attendeesFactory'];

        $scope.setSelectedLectures = function () {

            var lecture1, lecture2;

            for (var i = $scope.lectures.length - 1; i >= 0; i--) {
                var lecture = $scope.lectures[i];

                if (lecture.selected === true) {
                    if (lecture1 == null) {
                        lecture1 = lecture.lectureId;
                    }
                    else {
                        lecture2 = lecture.lectureId;
                    }
                }
            }

            attendeesFactory.setSelectedLectures($scope.emailAddress.text, lecture1, lecture2).then(function (data) {
                $scope.showInvalidUserMessage = true;
                $scope.message = data.message;
            });
        };

        activate();

        function activate() { }
    }
})();

attendessFactory.js

(function () {
    'use strict';

    angular
        .module('app')
        .factory('attendeesFactory', attendeesFactory);

    attendeesFactory.$inject = ['$http'];

    function attendeesFactory($http) {
        var service = {
            setSelectedLectures: setSelectedLectures
        };

        return service;

        function setSelectedLectures(emailAddress, lecture1, lecture2) {
            var promise = $http({
                method: 'POST',
                url: '/Home/SetSelectedLectures',
                data: {
                    emailAddress: emailAddress,
                    lecture1: lecture1,
                    lecture2: lecture2
                }
            }).then(function (response) {
                console.log(response);
                return response.data;
            });

            return promise;
        }
    }
})();

And my MVC Controller:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public IActionResult SetSelectedLectures(SelectedLectureData data)
    {
        // ...
    }
}

public class SelectedLectureData
{
    public String EmailAddress { get; set; }
    public int Lecture1 { get; set; }
    public int? Lecture2 { get; set; }
}

I've tried what some posts on StackOverflow suggested, such as using JSON.stringify, changing the content-type, but I still get the parameter values null (even if I put them directly in the action, instead of using a custom class).

3 Answers 3

1

Use [FromBody] anotation to make it working which will serialize data in SelectedLectureData model.

public IActionResult SetSelectedLectures([FromBody]SelectedLectureData data)

Otherwise you need to do

    var promise = $http({
        method: 'POST',
        url: '/Home/SetSelectedLectures',
        data: JSON.strigify({ "data": {
            emailAddress: emailAddress,
            lecture1: lecture1,
            lecture2: lecture2
        }})
    })
Sign up to request clarification or add additional context in comments.

1 Comment

You were right. All I needed was the [FromBody] annotation.
0

Try updating your javascript to

function setSelectedLectures(emailAddress, lecture1, lecture2) {
    var model = {
            emailAddress: emailAddress,
            lecture1: lecture1,
            lecture2: lecture2
        };
    var data = JSON.strigify(model);
    var promise = $http({
        method: 'POST',
        url: '/Home/SetSelectedLectures',
        data: data
    }).then(function (response) {
        console.log(response);
        return response.data;
    });

    return promise;
}

and using [FromBody] attribute on controller action

[HttpPost]
public IActionResult SetSelectedLectures([FromBody]SelectedLectureData data)
{
    // ...
}

Comments

0

JSON property name should be same as class properties else it will take it as null

 function setSelectedLectures(emailAddress, lecture1, lecture2) {
        var promise = $http({
            method: 'POST',
            url: '/Home/SetSelectedLectures',
            data: {
                EmailAddress : emailAddress,
                Lecture1: lecture1,
                Lecture2: lecture2
            }
        }).then(function (response) {
            console.log(response);
            return response.data;
        });

1 Comment

The default databinder is not case sensitive.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.