Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post a collection to WebApi using Restangular.Js

This get posted to the server as departments are null

JavaScrtipt code:

var departments = [{ DepartmentName : "Name", DepartmentId : 1}];
Restangular.all('records/StartNewDate').post(departments);

Web Api Controller

public HttpResponseMessage StartNewDate(DepartmentViewModel[] departments)
{
  ....
  ....
}

Server Model

public class DepartmentViewModel
{
    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }
}
like image 649
eugenekgn Avatar asked Jan 24 '14 21:01

eugenekgn


1 Answers

You have to JSON.stringify the object before you send it in the payload:

 return Restangular.all('records/StartNewDate').post(JSON.stringify(departments));
like image 81
parliament Avatar answered Sep 18 '22 02:09

parliament