My model:
public class Company
{
[BsonElement]
[BsonRepresentation(MongoDB.Bson.BsonType.String)]
public String PlaceId { get; set; }
[BsonElement]
[BsonRepresentation(MongoDB.Bson.BsonType.String)]
public string Name { get; set; }
[BsonElement]
[BsonRepresentation(MongoDB.Bson.BsonType.String)]
[BsonIgnoreIfNull]
public string Email { get; set; }
[BsonElement]
[BsonRepresentation(MongoDB.Bson.BsonType.Double)]
public Double Rating { get; set; }
[BsonElement]
[BsonIgnoreIfNull]
public Department Department { get; set; }
[BsonElement]
[BsonIgnoreIfNull]
public Product Product { get; set; }
[BsonElement]
public Comment[] Comments { get; set; }
}
public class Comment
{
[BsonElement]
public String Text { get; set; }
}
My controller method:
public JsonResult SavePlace(Company company)
{
if (company != null)
{
var client = new MongoClient("mongodb://localhost");
var database = client.GetDatabase("mongogoogleplace");
var placeData = database.GetCollection<BsonDocument>("googledatanew");
var department = company.Department.ToBsonDocument();
var product = company.Product.ToBsonDocument();
//var comments = company.Comments.ToBsonElement();
var companyModel = company.ToBsonDocument();
var filter = Builders<BsonDocument>.Filter.Eq("PlaceId", company.PlaceId);
var projection = Builders<BsonDocument>.Projection
.Exclude("_id");
//BsonDocument document = new BsonDocument();
var document = placeData.Find(filter).Project(projection).FirstOrDefault();
var documentJson = document.ToJson();
return Json(documentJson);
}
else
{
return Json(new { data = "error." });
}
}
Javascript snippet:
var company = { "PlaceId": PlaceId, "Name": Name, "Rating": Rating, "Comments": [{ Comment: { Text: '' } }, { Comment: { Text: '' } }, { Comment: { Text: '' } }, { Comment: { Text: '' } }, { Comment: { Text: '' } } ] };
for (var i = 0; i < CommentsArray.length; i++) {
company.Comments[i].Comment.Text = CommentsArray[i];
};
$.ajax({
type: "POST",
url: "../Home/SavePlace",
data: company,
// dataType: "json",
success: function (data){}
But every time I get the comments to be null.