I have been rewriting my angular vm to use the $resource module for my services calls. This has mostly worked, however I am having some issues when sending an array inside of an object, it seems that the array in question is always null once it is serialised on the server.
This create method was working previous to my refactoring of the code on both the VM and services, so I am a little unsure as to where the issue is taking place, however I believe it is an issue with the C# objects defined below.
var post = {};
post.Title = $scope.model.newPostItem.post_title;
post.Type = $scope.model.newPostItem.post_type;
post.Privacy = $scope.model.newPostItem.post_privacy;
post.UserId = $scope.model.user.userId;
post.PostContents = GetPostContentFromHtml($scope.model.newPostItem.post_content);
post.CreatedDate = new Date();
//We are making an ajax call so we set this to true
$scope.model.isAjaxInProgress = true;
admin.save({ post: post, userId: $scope.model.user.id, accessKey: $scope.model.user.accessKey }, { post: post },
function (data) {
// Add the post to the top of the maintenance page.
$scope.model.posts.unshift(data);
$scope.model.showCreate = false;
//We are done with AJAX loading
$scope.model.isAjaxInProgress = false;
ResetNewPostItem();
},
function (data) {
// called asynchronously if an error occurs
// or server returns response with an error status.
$scope.model.errorMessage = "Error occurred status:" + data.status + "-" + data.statusText;
//We are done with AJAX loading
$scope.model.isAjaxInProgress = false;
});
PostContents is my problem, this is an array of objects, this appears to be constructed fine and running a trace I end up with something like the following JSON.
{"post":{"Title":"This is a title","Type":0,"Privacy":2,"PostContents":[{"Style":"","Type":0,"Position":0,"Columns":3,"Content":"This is a test of the post data 1"},{"Style":"","Type":0,"Position":0,"Columns":3,"Content":"This is a test of the post data 2"},{"Style":"","Type":0,"Position":0,"Columns":3,"Content":" "},{"Style":"","Type":0,"Position":0,"Columns":3,"Content":"This is a test of the post data 3"}],"CreatedDate":"2015-05-16T06:24:53.446Z"}}
I realise that this isn't the most pleasant thing to read. However it should match up with the objects I am populating on the service.
[JsonObject(IsReference = true)]
[DataContract(IsReference = true)]
public class Post : IPost
{
public Post() { }
public Post(DBPost dbm)
{
if (dbm == null)
{
return;
}
this.User = new User(dbm.User);
this.UserId = dbm.UserId;
this.CommentGroupId = dbm.CommentGroupId;
this.Comments = new List<IComment>();
if (dbm.CommentGroup != null)
{
// Only take the first 5, don't be crazy!
foreach (var comment in dbm.CommentGroup.Comments.OrderByDescending(o => o.Id).Take(5))
{
this.Comments.Add(new Comment(comment));
}
}
this.PostContents = new List<PostContent>();
if (dbm.PostContents != null)
{
foreach (var item in dbm.PostContents)
{
this.PostContents.Add(new PostContent(item));
}
}
this.CreatedDate = dbm.CreatedDate;
this.Id = dbm.Id;
this.IsActive = dbm.IsActive;
this.Privacy = (Enums.Privacy)Enum.ToObject(typeof(Enums.Privacy), dbm.Privacy);
this.Title = dbm.Title;
this.Type = (Enums.PostType)Enum.ToObject(typeof(Enums.PostType), dbm.Type);
}
public int? CommentGroupId { get; set; }
public virtual ICollection<IComment> Comments { get; set; }
[DataMember]
public DateTime CreatedDate { get; set; }
[Key]
public int Id { get; set; }
[DataMember]
public bool IsActive { get; set; }
[DataMember]
public ICollection<PostContent> PostContents { get; set; }
[DataMember]
public Enums.Privacy Privacy { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public Enums.PostType Type { get; set; }
[JsonIgnore]
[IgnoreDataMember]
public IUser User { get; set; }
[DataMember]
public int? UserId { get; set; }
}
[JsonObject(IsReference = true)]
[DataContract(IsReference = true)]
public class PostContent : IPostContent
{
public PostContent(DBPostContent dbm)
{
if (dbm == null)
{
return;
}
this.AttachedTo = dbm.AttachedTo;
this.Columns = dbm.Columns;
this.Content = dbm.Content;
this.Id = dbm.Id;
this.Position = (Enums.Position)Enum.ToObject(typeof(Enums.Position), dbm.Position);
this.PostId = dbm.PostId;
this.Style = dbm.Style;
this.Type = (Enums.ContentType)Enum.ToObject(typeof(Enums.ContentType), dbm.Type);
}
[DataMember]
public int? AttachedTo { get; set; }
[DataMember]
public int Columns { get; set; }
[DataMember]
public string Content { get; set; }
[DataMember]
public object ContentHolder { get; set; }
[Key]
public int Id { get; set; }
[DataMember]
public Enums.Position Position { get; set; }
// [DataMember]
public int PostId { get; set; }
[DataMember]
public string Style { get; set; }
[DataMember]
public Enums.ContentType Type { get; set; }
This issue has started to appear following changes which were made on both the service tier as well as the Angular VM. I am unsure where the problem is and having been digging into it without success for a little while now. My service is setup as below.
angular.module('mySol.services').factory('admin', function ($resource) {
// return $resource('/api/maintenance/:id');
return $resource('/api/maintenance/:id', null,
{
//'update': { method: 'PUT' }
'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
});
Any assistance would be appreciated.