If there is no explicit need for a class (model), you can also use dynamic
List<dynamic> models = JsonConvert.DeserializeObject<List<dynamic>>(jsonString);
And then use dot to access whatever fields you need
models[0].MyField
Please make sure that all properties are both the getter and setter. In case, any property is getter only, it will cause the reverting the List to original data as the JSON string is typed.
Please refer to the following code snippet for the same:Model:
public class Person{public int ID { get; set; }// following 2 lines are cause of error//public string Name { get { return string.Format("{0} {1}", First, Last); } }//public string Country { get { return Countries[CountryID]; } }public int CountryID { get; set; }public bool Active { get; set; }public string First { get; set; }public string Last { get; set; }public DateTime Hired { get; set; }}public class ModelObj{public string Str { get; set; }public List<Person> Persons { get; set; }}
Controller:
[HttpPost]public ActionResult Index(FormCollection collection){var data = new ModelObj();data.Str = (string)collection.GetValue("Str").ConvertTo(typeof(string));var personsString = (string)collection.GetValue("Persons").ConvertTo(typeof(string));using (var textReader = new StringReader(personsString)){using (var reader = new JsonTextReader(textReader)){data.Persons = new JsonSerializer().Deserialize(reader, typeof(List<Person>)) as List<Person>; }}return View(data);}
Try to change type of ScoreIfNoMatch, like this:
public class MatrixModel{public string S1 { get; set; }public string S2 { get; set; }public string S3 { get; set; }public string S4 { get; set; }public string S5 { get; set; }public string S6 { get; set; }public string S7 { get; set; }public string S8 { get; set; }public string S9 { get; set; }public string S10 { get; set; }// the type should be stringpublic string ScoreIfNoMatch { get; set; }}
The variables/parameters within the class
definition requires { get; set; }
I was using like a variable declaration (stupid of me, because it was working for other scenarios) without
{ get; set; }
Because of which, whatever I send from the JavaScript, it was not being received in the Action method. It was always getting null or empty model.
Once the {get; set;} is added, it worked like charm.
I hope it helps someone who is coming from VB6 style of programming line.