0

I have an object like this:

var lot = { ID: 0, ... //various other attributes
            SchemeInstanceList: [] }

After creation I fill the SchemeInstanceList attribute with objects of another structure like this:

lot.SchemeInstanceList.push({ ID: 0, LotID: 0, SchemeID: 18, LotPosition: 0})

Until here, everything is fine. Now I send the lot object to my ActionMethod, creating a database entry.

$.ajax({
    url: "/Lots/CreateSchemes",
    data: lot
});

public ActionResult CreateSchemes(LotModel lot) {
    ... //store lot in database
}

The ActionMethod gets called and the values of lot are correct, there are entries in the SchemeInstanceList. All of these entries' attributes are zero however and I don't see what I'm doing wrong.

When I pass a single list element itself, the entries appear correctly:

$.ajax({
    url: "/Lots/CreateScheme",
    data: lot.SchemeInstanceList[0]
}

public ActionResult CreateScheme(SchemeInstanceModel scheme) {
   ... //do whatever
}

Now the SchemeID value is 18 instead of 0.

Why isn't this working? What most obvious thing am I missing?

EDIT: Here are my models:

public class LotModel {
    public int ID { get; set; }
    ... //various other properties
    public List<SchemeInstanceModel> SchemeInstanceList { get; set; }
}

public class SchemeInstanceModel
{
    public int ID { get; set; }
    public int LotID { get; set; }
    public int SchemeID { get; set; }
    public int LotPosition { get; set; }

    //public List<AttributeInstanceModel> AttributeInstanceList { get; set; }
}
tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

0

Try this:

public ActionResult CreateSchemes(string lot) {
    LotModel li = JsonConvert.DeserializeObject<LotModel>(lot);
    ... //store lot in database
}

Check if that string contains the values you want...

Also make sure your LotModel has a List property.

My guess is that if the values are in the string being passed in, then your javascript LotModel does not match you C# LotModel. Remember, the model property names have to be exactly the same in javascript as in C#, capital letter and all.

Danwilliger
  • 1,221
  • 10
  • 7
  • Okay, I don't know what has changed since yesterday but now my SchemeInstanceList is always null. I've checked the property name and all the SchemeInstanceModel's property names and they all match. The string transmitted contains the list with SchemeInstances and their values correctly. – Otto Abnormalverbraucher Mar 21 '13 at 09:36
  • I've called the function with a string like `data: { lot: lotjson }` and on returning I forgot to change it back. `data: { lot: lot }` caused every property to be null. Anyway, now that I'm back to the former state. Within my SchemeInstanceModel there's another list, AttributeInstanceList, which I did not add in JavaScript. However, adding an empty AttributeInstanceList to my SchemeInstance or removing the List from the model did not change anything. – Otto Abnormalverbraucher Mar 21 '13 at 10:16
  • It's hard for me to give anymore help without seeing your models. If the string contains all the data then I can only guess there is an error in one of your models. However, maybe you're passing an alpha character into an integer or something. – Danwilliger Mar 21 '13 at 16:23
  • Yeah, you're right, I'm sorry for not having added the Models. However, I already answered my own question ;) But thank you anyways. – Otto Abnormalverbraucher Mar 22 '13 at 10:13
0

Found the solution here:

https://stackoverflow.com/a/5373956/2139555

In order to make json binding for complex objects work, one needs to JSON.stringify(lot) his data and set contentType: "application/json, charset=utf-8". Also I didn't set type: "Post", but I had tried that and it had not worked when I did. Besides I got the data for the lot so I thought it would not matter.

Complete working json:

$.ajax({
    url: "/Lots/CreateSchemes",
    data: JSON.stringify(lot),
    type: "Post",
    dataType: "application/json, charset=utf-8"
});
Community
  • 1
  • 1