-1

I am using asp.net core MVC 3.1.0. I make a call from AngularJS to MVC controller to submit form. But the parameter object is passing null to controller:

AngularJS:

$scope.minfo = 0;
$scope.submit = function () {   
    var model = {"obj": {"Email": $scope.email,"Password": $scope.pw,"memberInformed":$scope.minfo};       
    return $http({
        method: 'POST',
        url: '/Home/Register',
        data: model
    });
};

Controller c#:

[AllowAnonymous]
[HttpPost]
public IActionResult Register(Register obj)
{       
    //...
    return View();
}

Update: boolean input field:

<input type="checkbox" name="minfo" ng-model="minfo" ng-true-value="1" ng-false-value="0" required />

I can send parameters without memberInformed boolean parameter. Otherwise the object is null. Thanks for the answers.

Zeynep
  • 159
  • 1
  • 15
  • Which version of MVC are you using? Please try modifying controller action signature to use [FromBody] . => `public IActionResult Register([FromBody]Register obj)`. – sam Feb 28 '20 at 14:33
  • There is no need to stringify the data. The AngularJS frame work does that automatically. – georgeawg Feb 28 '20 at 14:35
  • Without stringfy it shows 'Object object', but I can see parameters with stringfy. The problem is they are null in the controller side. @georgeawg – Zeynep Feb 28 '20 at 14:41
  • Check the network tab of the Developer Console to see the request and debug it. – georgeawg Feb 28 '20 at 14:50
  • I updated my question @sam [FromBody] is not working – Zeynep Feb 28 '20 at 14:51
  • Does this answer your question? [Post parameter is always null](https://stackoverflow.com/questions/10984040/post-parameter-is-always-null) – Julien Feb 28 '20 at 15:02
  • @Zeynep, Thank you for updating the answer. I see obj is wrapped inside object (`{}`) again. could you just try sending {"Email": $scope.email,"Password": $scope.pw } to controller – sam Feb 28 '20 at 15:09
  • 1
    using `ng-true-value` , minfo value won't be a boolean – Bill P Feb 28 '20 at 16:11
  • When I remove them, if the field is unchecked, cannot get the value from checkbox @BillP – Zeynep Feb 28 '20 at 16:20

1 Answers1

0

Since your controller action public IActionResult Register(Register obj) is taking Register viewmodel, please modify your angular code to send the obj without wrapping it inside {} as below:

$scope.submit = function () {   
    var model = {"Email": $scope.email,"Password": $scope.pw };       
    return $http({
        method: 'POST',
        url: '/Home/Register',
        data: model
    });
};

UPDATE: based on your update, you are trying to send int ($scope.minfo) to boolean data type memberInformed and hence the issue.

So, either change C# Register object's memberInformed property type from bool to int as shown below;

public class Register
{
    // Since you are sending integer value from angular, modify the data type to int from bool
    // public bool memberInformed { get; set; } 
    public int memberInformed { get; set; } 


    public string Email { get; set; } 

    public string Password { get; set; } 
}

or modify your angular and view code as below:

$scope.minfo = false;

<input type="checkbox" ng-model="checkboxModel.value2"  ng-true-value="true" ng-false-value="false">

or you can skip ng-true-value and ng-false-value as below:

<input type="checkbox" ng-model="checkboxModel.value2">
sam
  • 1,937
  • 1
  • 8
  • 14
  • Sorry. As I was trying to shorten my question I've removed some parameters. I see that a bool parameter is the problem. I updated my question again. Your answer works without json.stringify by the way. – Zeynep Feb 28 '20 at 15:53
  • @Zeynep: I updated my answer to remove Json.stringify . Also, explained the issue with memberInformed. Please verify the Update section in my answer – sam Feb 28 '20 at 17:08