1

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.

1
  • 1
    First of all, force the attribute [HttpPost] on your action "SavePlace". Try using List<Comment> instead of an array. Then in your Ajax query put "application/json; charset=utf-8" for contentType and "JSON.stringify(company)" for data Commented Jan 28, 2016 at 8:24

3 Answers 3

2

In your data you need to convert object to JSON in this way:

data: JSON.stringify(company)

Now in your method you should be able to get the comments. Another way is this:

data: { company: company }

Where the first name must be the same as your parameter name in the action method. I'm not sure at 100% that works because I'm not sure that company will be converted to a C# object correctly.

1

Change:

data:  company

To

data : {company: company}

The Action expects an object with a parameter named company.

2
  • Still it's not working, moreover, all the other values of the json becomes null as well. Commented Jan 28, 2016 at 8:28
  • Please show the model of Comment and what is being sent to the server. Commented Jan 28, 2016 at 8:38
0

Your Action must look like this:

[HttpPost]
public JsonResult SavePlace(Company company)
{
    // Your code
}

Your Ajax request:

$.ajax({
    type: "POST",
    url: "../Home/SavePlace",
    data:  JSON.stringify(company),
    contentType: "application/json; charset=utf-8",
    success: function (data){}
});

Define your company object like this:

var company = {
    PlaceId: "XXXX", 
    Name: "XXXX", 
    Rating: 10.0, 
    Comments: [
        Comment: { Text: '' },
        Comment: { Text: '' },
        Comment: { Text: '' },
        Comment: { Text: '' },
        Comment: { Text: '' }
    ]
};

Here is a complete working example. The code of the view:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
    var company = {
        PlaceId: "XXXX", 
        Name: "XXXX", 
        Rating: 10.0, 
        Comments: [
            Comment: { Text: '' },
            Comment: { Text: '' },
            Comment: { Text: '' },
            Comment: { Text: '' },
            Comment: { Text: '' }
        ]
    };

    $(document).ready(function(){
        $("#toto").on("click", function () {
            $.ajax({
                type: "POST",
                url: "../Home/SavePlace",
                data:  JSON.stringify(company),
                contentType : "application/json; charset=utf-8",
                dataType: "json",
            });
        });
    });

</script>
<input type="button" id="toto" />

The c# and controller code:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return this.View();

    }

    [HttpPost]
    public JsonResult SavePlace(Company company)
    {
        if (company != null)
        {

            return Json(new { data = "fine." });
        }
        else
        {
            return Json(new { data = "error." });
        }
    }
}

public class Company
{
    public String PlaceId { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }

    public Double Rating { get; set; }

    public object Department { get; set; }

    public object Product { get; set; }

    public Comment[] Comments { get; set; }
}

public class Comment
{
    public String Text { get; set; }
}
3
  • All the values sent to server side still show null. Commented Jan 28, 2016 at 8:53
  • Try with unquoting property name in your JS company object. Commented Jan 28, 2016 at 8:54
  • I edited the answer because the Comments array definition in JS is faulty. Commented Jan 28, 2016 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.