Ajax和MVC4 - 如何在另一个对象中传递对象列表

时间:2014-09-19 00:43:38

标签: jquery ajax asp.net-mvc-4 post asp.net-ajax

我一直在努力从一个视图传递给一个对象,该对象在其内部具有一个对象列表作为属性。它就像:

ModelA = 
 {
  PropertyA,
  PropertyB,
  List<ModelB>
 }
ModelB = 
 {
  Property1,
  Property2
 }

我尝试使用AJAX,如$ .toJSON,$ .toDictionary。

同样使用[0] ModelB,[1] ModelB以避免使用AJAX。

我尝试的方式并不重要我总是将ModelA.ModelB作为空列表接收。所有其他属性都可以。

我在这上面花了太多时间......

编辑: 请求是从客户端到服务器。在这里你有我如何做这个帖子(至少我试过的方式之一)

var model =
{
  Cost: $("#Cost").val(),
  Time: $("#Time").val(),
  Resources: $("#Resources").val(),
  Feedstocks: []
};
$.each($(".selected"), function (index, item) {
  model.Feedstocks.push({
    ProductID: $("#ProductID").val(),
    Scrap: $(this).find(".scrap").val(),
    Quantity: $(this).find(".quantity").val()
  });
});
$.ajax({
  type: 'POST',
  url: "Products/AddProcess",
  contentType: 'application/json',
  data: JSON.stringify(model),
  dataType: 'json',
  success: function (data) {
    if (data.Result) {
      window.location.href = "/Products/Details/" + $("#ProductID").val();
    }
    else {
      alert(data.Value);
    }
  }
})

0 个答案:

没有答案
相关问题