My raw json string passed to the MVC ActionResult Controller via AJAX post
{"ID":0,"RoutingRuleID":24,"ConditionalType":0,"Field":"Channel","ConditionalOperator":"5","Values":[1,9],"ValueString":""}
But what ends up happening is that once the json objects gets to the MVC controller it loses the values in the Associated Array "Values". The other properties are set correctly.
My model Class in C# is as follows:
public class RoutingConditional
{
public int ID { get; set; }
public int ParentID { get; set; }
public string ConditionalType { get; set; }
public string Field { get; set; }
public string ConditionalOperator { get; set; }
public List<string> Values { get; set; }
public string ValueString{get;set;}
public RoutingConditional()
{
//this.Values = new List<string>(); //I tried to initialize it too did not work
}
}
My MVC Controller
[HttpPost]
public ActionResult EditConditional(RoutingConditional rcview)
{
//rcview.Values = null
}
My Javascript
$.ajax({
url: actionURL,
type: "post",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(myModel.RoutingConditional),
........standard success and error
});
Why is it being passed in as null for the array(List)?
This is a weird one, not sure I can fully explain (have an idea) but here's what I did.
Stripped out all your json parameters in the payload except the "Values":[1,9]
and it worked just fine.
So started adding back each json parameter starting at the end (luckily). When I re-added "ValueString":""
it crapped out again.
So added a few more json params to see if it was an ordering issue (e.g., nothing can go after the array). That wasn't the case.
So started renaming stuff and when I renamed "ValueString":""
to something like "TmpValueString":""
it worked again.
Here's my best guess. The word ValueString has pieces of the name that match the first characters another property. In this instance, "values-tring" matches with "values" (array name) thereby throwing the MVC binder off when it goes to match against your object model. I'm not 100% on this, but that's what it seems.
So your solution is to rename one of your props so that its name does not make up the first characters of another prop.
Also, wanted to mention ConditionalOperator and ConditionalType names to counter any arguments. These names are unique in that they are not subsets of each other, but merely contain like characters. Whereas Values is a subset of Valuestring thus causing, what I think, is binding confusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With