0

I have been traying to pass from a view to the action a object that inside its it have as a property a list of object. It is like:

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

I tried all work around with AJAX like $.toJSON, $.toDictionary.

Also using [0]ModelB, [1]ModelB to avoid using AJAX.

It doesn't matter how I tried I always received ModelA.ModelB as an empty list. All the other properties are ok.

I'm wating too much time on this....

EDIT: The request is from client to server. Here you have how I'm doing the post (at least one of the way I tried)

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);
    }
  }
})
Faabass
  • 1,394
  • 8
  • 29
  • 58
  • you mean from server to client or client to server – Steve Sep 19 '14 at 00:47
  • forgot what I did exactly to solve it. think im using something called newton json – Steve Sep 19 '14 at 01:24
  • Do you have to use Ajax?, if your using MVC you can just pass an anonymoustype to the action – sa_ddam213 Sep 19 '14 at 01:28
  • How? I'm using Ajax, because I am adding elements to a table with jQuery. So I need to send an object which contains each row of the table as another object. I tried to do name="[0].Feedstocks.ProductID" in each input in the table... But it didn't work – Faabass Sep 19 '14 at 01:48
  • @Faabass, You can use a standard submit for this, it's just a matter of naming your inputs correctly when you dynamically add them. [This answer](http://stackoverflow.com/questions/24026374/adding-another-pet-to-a-model-form/24027152#24027152) might help. –  Sep 19 '14 at 02:36
  • So you mean something like name="ModelB[0].ProductID" ? – Faabass Sep 19 '14 at 03:18
  • I make the change and now I have: My real object is public class ProductProcessModel { public List Feedstocks = new List(); } public class FeedstockModel { public int ProductID { get; set; } public double Scrap { get; set; } public double Quantity { get; set; } } But I'm still getting the Feedstocks as an empty list. – Faabass Sep 19 '14 at 03:23
  • @Faabass, You cant use `public List Feedstocks = new List();` - it has no setter so it cant be bound. Change it to `public List Feedstocks { get; set;}` You can initialize it the constructor if you want. ps. if you want me to be notified of a message, begin it as I have here –  Sep 19 '14 at 10:26

0 Answers0