asp.net mvc - Computed Property not calculated when passing as parameter in $http.post in AngularJS -
i creating object in jquery contains computed properties. sending object web api service object defined class object. properties $scope variable set except computed variable appears null mean values not calculated when send part of $http.post method. please see code.
jquery code
$scope.estimationefforts = { //total no. of apps distribution totalappsdistributionnumbers: [ { description: "number of no issue apps", value: function () { return ($scope.total_no_apps * $scope.calculationasssumptions.expriencebasedassumptions[0].ratio) / 100 } }, { description: "number of apps vendor upgrade", value: function () { return ($scope.total_no_apps * $scope.calculationasssumptions.expriencebasedassumptions[1].ratio) / 100 } }, { description: "number of bespoke apps not requiring remediation", value: function () { return ($scope.total_no_apps * $scope.calculationasssumptions.expriencebasedassumptions[2].ratio) / 100 } }, { description: "number of bespoke apps requiring remediation", value: 12 } ]}
now sending variable in post call shown below..
var effortmodel = $scope.estimationefforts; $http.post('/effortestimate/generateexcel', $scope.estimationefforts).success(function (data) { alert(data); });
on server side sample code this...
public class effortmodel { public list<appdetails> totalappsdistributionnumbers { get; set; } } public class appdetails { public string description { get; set; } public float? value { get; set; } } [httppost] public actionresult generateexcel(effortmodel objeffortmodel) { float? ss = objeffortmodel.totalappsdistributionnumbers[0].value; return json(ss, jsonrequestbehavior.allowget); }
on server description appears values (computed properties) appearing null. contains values on client side before call goes server side can see through firebug read value() means act function.
how can values on server side?
you cannot send function
objects parameters in post method, because there's no corresponding .net type can converted. function
stored in value
property not automatically evaluated before data gets sent.
i'll assume use object inside angularjs controller. if true, should create "companion" object, stores functions , original object should store computed values:
var tmptotalappsdistributionnumbers = [ { description: "number of no issue apps", value: function () { return ($scope.total_no_apps * $scope.calculationasssumptions.expriencebasedassumptions[0].ratio) / 100 } }, { description: "number of apps vendor upgrade", value: function () { return ($scope.total_no_apps * $scope.calculationasssumptions.expriencebasedassumptions[1].ratio) / 100 } }, { description: "number of bespoke apps not requiring remediation", value: function () { return ($scope.total_no_apps * $scope.calculationasssumptions.expriencebasedassumptions[2].ratio) / 100 } }, { description: "number of bespoke apps requiring remediation", value: 12 } ]} $scope.estimationefforts = { //total no. of apps distribution totalappsdistributionnumbers: [ ] } //this should called before making post request (var = 0; < tmptotalappsdistributionnumbers.length; i++){ var val = 0; if (typeof tmptotalappsdistributionnumbers[i].value === "function") { val = tmptotalappsdistributionnumbers[i].value(); } else { val = tmptotalappsdistributionnumbers[i].value; } $scope.estimationefforts.totalappsdistributionnumbers.push( { description: tmptotalappsdistributionnumbers[i].description, value: val } ); }
if you're not going use object before posting, can use computation without wrapping in inside function.
Comments
Post a Comment