Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC 4 - ModelBinder bug
    text
    copied!<p>So I was playing around with my ASP.NET MVC 4 solution. Every thing worked fine kept adding things but something odd started happening.</p> <p>One of my Models properties was null, even though I had items in the Json passed to it.</p> <p>This was the javascript object/json passed it it:</p> <pre><code>var obj = { "plc": "False", "al": ["386", "710"], "pl": ["9530", "211", "783"] }; </code></pre> <p>I was using a Custom Model binder ... thought that might be the issue so I turned it off.</p> <p>Tried using the JavaScriptSerializer from .NET to see it that worked:</p> <pre><code>var reader = new StreamReader(Request.InputStream); Request.InputStream.Position = 0; var readToEnd = reader.ReadToEnd(); var javaScript = new JavaScriptSerializer(); var searchFarmOptions = javaScript.Deserialize&lt;Test&gt;(readToEnd); </code></pre> <p>Got all the properties set ... WOOT.</p> <p>So I tried a clean ASP.NET MVC 4 solution. To reproduce the bug.</p> <h3>This is from the Index.cshtml view</h3> <pre><code>@{ ViewBag.Title = "title"; } &lt;h1&gt;Title&lt;/h1&gt; Testing ... &lt;script src="/Scripts/jquery-1.8.2.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script&gt; $(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); }); }); &lt;/script&gt; </code></pre> <h3>This is my HomeController</h3> <pre><code>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&lt;int&gt; PL { get; set; } public List&lt;int&gt; AL { get; set; } public bool PLC { get; set; } public bool ALC { get; set; } } } </code></pre> <p>Yes, the bug is still there.</p> <p>Whenever I have a property starting with "pl" as my list name is .. the "pl" list is null.</p> <p>Also, it could be any name starting with "pl" ... like "plchecked"</p> <p>If I rename the "plc" to "cpl" its working.</p> <p>So what is going on here ... is there any naming restrictions in the model binder? What am I missing here?</p> <h2>Update 1</h2> <h3>Work</h3> <p>PL server side now have the right values, etc. not null, but the list of numbers.</p> <pre><code>var obj = { "pl": ["9530", "211", "783"], "1plc": "false", "pl-some-odd-value": "false", "al": ["386", "710"], "alc": "false" }; </code></pre> <h3>Don't work</h3> <p>PL server side now have <code>null</code> value.</p> <pre><code>var obj = { "pl": ["9530", "211", "783"], "al": ["386", "710"], "alc": "false", "pl-odd-value": "false" }; </code></pre> <h3>Work</h3> <p>PL has the 3 values som the json object string ... </p> <pre><code>var obj = { "pl": ["9530", "211", "783"], "al": ["386", "710"], "alc": "false", "odd-value-pl": "false" }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload