So I was playing around with my ASP.NET MVC 4 solution. Every thing worked fine kept adding things but something odd started happening.
One of my Models properties was null, even though I had items in the Json passed to it.
This was the javascript object/json passed it it:
var obj = {
"plc": "False",
"al": ["386", "710"],
"pl": ["9530", "211", "783"]
};
I was using a Custom Model binder ... thought that might be the issue so I turned it off.
Tried using the JavaScriptSerializer from .NET to see it that worked:
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 bug.
This is from the Index.cshtml view
@{
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 bug is still there.
Whenever I have a property starting with "pl" as my list name is .. the "pl" list is null.
Also, it could be any name starting with "pl" ... like "plchecked"
If I rename the "plc" to "cpl" its working.
So what is going on here ... is there any naming restrictions in the model binder? What am I missing here?
Update 1
Work
PL server side now have the right values, etc. not null, but the list of numbers.
var obj = {
"pl": ["9530", "211", "783"],
"1plc": "false",
"pl-some-odd-value": "false",
"al": ["386", "710"],
"alc": "false"
};
Don't work
PL server side now have null value.
var obj = {
"pl": ["9530", "211", "783"],
"al": ["386", "710"],
"alc": "false",
"pl-odd-value": "false"
};
Work
PL has the 3 values som the json object string ...
var obj = {
"pl": ["9530", "211", "783"],
"al": ["386", "710"],
"alc": "false",
"odd-value-pl": "false"
};
PLCis setPLis always null.Update 1... I'm just wondering if the ModelBinder does some magic with some property names.TryUpdateModelmethod within a ActionMethod is working. I'm all out of words and the ModelBinder stuff is a bit complex for me.