So, I played with my ASP.NET MVC 4 solution. Everything worked fine, adding things, but something weird started.
One of the properties of my models was null, although I had elements in Json passed to it.
It was a javascript / json object that passed it:
var obj = { "plc": "False", "al": ["386", "710"], "pl": ["9530", "211", "783"] };
I used the Custom Model binding ... thought it might be a problem, so I disabled it.
Tried using JavaScriptSerializer from .NET to see how it works:
var reader = new StreamReader(Request.InputStream); Request.InputStream.Position = 0; var readToEnd = reader.ReadToEnd(); var javaScript = new JavaScriptSerializer(); var searchFarmOptions = javaScript.Deserialize<Test>(readToEnd);
Got all the properties set ... WOOT.
So, I tried a clean ASP.NET MVC 4 solution. To reproduce the error.
This is from the view of Index.cshtml
@{ ViewBag.Title = "title"; } <h1>Title</h1> Testing ... <script src="/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script> <script> $(function() { var obj = { "1pllort": "False", "1plc": "true", "al": ["386", "710"], "pl": ["9530", "211", "783"] }; var options = { "contentType": "application/json; charset=UTF-8", "type": "POST", "data" : JSON.stringify(obj) }; $.ajax("/Home/TestPost", options).done(function(data) { console.log(data); }); }); </script>
This is my HomeController
using System.Collections.Generic; using System.Web.Mvc; namespace MvcApplication3.Controllers { public class HomeController : Controller { public ActionResult Index() { return View("Index"); } [HttpPost] public ActionResult TestPost(Test model) { return Json(model); } } public class Test { public List<int> PL { get; set; } public List<int> AL { get; set; } public bool PLC { get; set; } public bool ALC { get; set; } } }
Yes, the error still exists.
Whenever I have a property starting with "pl" as the name of my list, the list "pl" is null.
Alternatively, it can be any name starting with "pl" ... like "plchecked"
If I rename "plc" to "cpl" its work.
So what is going on here ... are there any restrictions on naming in the connecting device? What am I missing here?
Update 1
Job
Now on the server side of PL there are correct values, etc., but not a zero, but a list of numbers.
var obj = { "pl": ["9530", "211", "783"], "1plc": "false", "pl-some-odd-value": "false", "al": ["386", "710"], "alc": "false" };
Does not work
Now on the server side of PL there is a null value.
var obj = { "pl": ["9530", "211", "783"], "al": ["386", "710"], "alc": "false", "pl-odd-value": "false" };
Job
PL has 3 values ββsom string json object ...
var obj = { "pl": ["9530", "211", "783"], "al": ["386", "710"], "alc": "false", "odd-value-pl": "false" };