angularjs - MVC 6 API Multiple Parameters -


i have following api, takes care of updating items in database:

    [route("update")]     [httppost("")]     public jsonresult updaterecords([frombody]icollection<shoppingitemviewmodel> vm)     {         if (modelstate.isvalid)         {             try             {                 var items = mapper.map<ienumerable<shoppingitem>>(vm);                  //update database                 _repository.updatevalues(items, user.identity.name);                  return json(null);             }             catch (exception ex)             {                 response.statuscode = (int)httpstatuscode.badrequest;                 return json(null);             }         }         else         {             response.statuscode = (int)httpstatuscode.badrequest;             return json(null);         }     } 

then under angular code executing post method following:

    $scope.savechanges = function () {         $http.post("/api/items/update", $scope.items)             .then(function (response) {             }, function (err) {                 $scope.errormessage = "error occured: " + err;             }).finally(function () {              });      }; 

what do, introduce new parameters initial updaterecords function, of them optional. depending on inserted parameters procedure different things.

what have tried change function following (example):

public jsonresult updaterecords([frombody]icollection<shoppingitemviewmodel> vm, [frombody]bool eraseolditems) 

and under angular app:

$http.post("/api/items/update", {vm:$scope.items, eraseolditems: true}) 

or even

$http.post("/api/items/update", {'vm':$scope.items, 'eraseolditems': true}) 

but not code work (my parameters time null).

what doing wrong here?

from parameter binding in asp.net web api:

at 1 parameter allowed read message body.

// caution: not work!     public httpresponsemessage post([frombody] int id, [frombody] string name) { ... } 

the reason rule request body might stored in non-buffered stream can read once.

you can pass request object contains other objects:

public class request {     public icollection<shoppingitemviewmodel> vm { get; set; }     public bool eraseolditems { get; set; } } 

and action:

[route("update")] [httppost("")] public jsonresult updaterecords([frombody]request request){ ... } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -