1

I have an object in angularjs which I want to pass and map it to custom c# class in mvc controller. but whenever I am doing this class object is null completely.

 $scope.Get = function () {
        var EService = [{
            id: $scope.Id,
            servicename: $scope.ServiceName,
            servicetype: $scope.ServiceType,
            monthlyrental: $scope.MonthlyRental,
            serviceremarks: $scope.ServiceRemarks,
            servicestatus: $scope.status,
            activationdate: $scope.ActivationDate,
            deactivationdate: $scope.DeActivationDate
        }];

        $http.post('/TS/API/Insert', Service).then(function (res) {
            debugger;
        })

MVC Controller and Class:

[HttpPost] 
    public string Insert(ServicesMaster Service)
    {

        GIBCADBEntities gfientity = new GIBCADBEntities();

        var record = "Sent"
        return Json(record, JsonRequestBehavior.AllowGet); 
    } public class ServicesMaster
{
    public string id { set; get; }
    public string servicename { set; get; }
    public string servicetype { set; get; }
    public int? monthlyrental { set; get; }
    public string serviceremarks { set; get; }
    public byte servicestatus { set; get; }
    public DateTime? activationdate { set; get; }
    public DateTime? deactivationdate { set; get; }
}

The javascript variable/object "EService" is ok here, and when passing only the ServicesMaster object is created with null values and no data is mapped to it. I can send single string or any value from here but when sending a complete object its behaving like this.

1 Answer 1

2

You are passing an array from front end and fetching object from server end. just remove the "[" and "]" brace while set value to EService . Like :

 $scope.Get = function () {
     var Service = {};
     Service = {
        id: $scope.Id,
        servicename: $scope.ServiceName,
        servicetype: $scope.ServiceType,
        monthlyrental: $scope.MonthlyRental,
        serviceremarks: $scope.ServiceRemarks,
        servicestatus: $scope.status,
        activationdate: $scope.ActivationDate,
        deactivationdate: $scope.DeActivationDate
     };

     $http.post('/TS/API/Insert', Service).then(function (res) {
        debugger;

     }); 
};

It should work now. :)

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

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.