Mahajan344 Mahajan344 - 6 months ago 7
AngularJS Question

model value in when passed from angular post service

I am working on AngularJs with MVC , I am having a silly issue while passing model to MVC controller.

If I Debug the code .. data is there in object till I make ajax post call.. but when I check on controller everything becomes null.

Here is my code

AngularController

$scope.loginUser = {
email: "",
password: "",
typeofUser: "ExternalUser"
}

var onLoginComplete = function (data) {
console.log(data);
};

var onError = function (reason) {
$scope.error = "Could not fetch the data.";
};

DataService.validateUser($scope.loginUser).then(onLoginComplete, onError);


Now DataService

var baseurl = window.location.protocol + "//" + window.location.host + "/";
var validateUser = function (model) {
return $http.post(baseurl + "Account/Login/" + model)
.then(function (response) {
return response.data;
});
}


and Model class in MVC

public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
public string Email { get; set; }

[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[Required]
public string TypeofUser { get; set; }
}


Now you can below image that before making post call to controller , object has values..
enter image description here

But after going to controller , all values are null

enter image description here

Win Win
Answer

Post parameters are not correct. Replace + with ,.

return $http.post(baseurl + "Account/Login/", model)

You can read more here.

$http.post('/someUrl', data, config).then(successCallback, errorCallback);
Comments