如何将JSON对象中的数组从javascript传递到asp.net mvc控制器方法?

时间:2016-01-28 08:18:13

标签: javascript c# asp.net json

我的模特:

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; }
}

我的控制器方法:

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片段:

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){}

但每次我都将评论视为空。

3 个答案:

答案 0 :(得分:1)

变化:

data:  company

data : {company: company}

Action需要一个名为company。

的参数的对象

答案 1 :(得分:1)

在您的数据中,您需要以这种方式将对象转换为JSON:

data: JSON.stringify(company)

现在,在您的方法中,您应该能够获得评论。另一种方式是:

data: { company: company }

其中名字必须与action方法中的参数名称相同。我不确定100%是否有效,因为我不确定该公司是否会正确转换为C#对象。

答案 2 :(得分:0)

你的行动必须如下:

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

您的Ajax请求:

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

像这样定义公司对象:

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

这是一个完整的工作示例。 视图的代码:

<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" />

c#和控制器代码:

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; }
}