This project is read-only.


Bug in MVC4: System.Web.PrefixContainer


It seems that internal System.Web.PrefixContainer class was introduced in MVC4 and this class has the bug, which is reproduced with the following model, view and controller action method:

public class Model
public Model()
    Value = new List<Item>();

public string SomeData { get; set; }

public string ValueIsSequence { get; set; }

public class Item
    public string Data { get; set; }

public List<Item> Value { get; set; }

@using (Html.BeginForm())
<input type="text" name="SomeData" value="123" />
<input type="text" name="ValueIsSequence" value="True" />
<input type="text" name="Value[0].Data" value="ABC" />

<button type="submit">OK</button>

public ActionResult Testing(Model model)
var failed = model.Value.Count == 0;
return View();

The bug is that Model.Value list is empty and "Value[0].Data" form field is skipped. Prefix matching fails while checking "Value" prefix (for the Model.Value property) and probing "ValueIsSequence" field in binary search: "Value" is not equal to "ValueIsSequence" of course, and then the PrefixContainer supposed (incorrectly) that field can only be "Value." or ""Value[", but it detects "ValueIsSequence" that does not match "Value." nor "Value[", the comparer returns -1 (less) and binary search is moving to less half to probing "SomeData" field. So the binary search never detects "Value[0].Data" and Model.Value stays not filled.

Renaming "ValueIsSequence" to "IsSequence" for example, is a possible workaround.

file attachments

Closed Jun 8, 2013 at 12:25 AM by jacalvar


McSyko wrote Nov 28, 2012 at 7:47 AM

I also had this issue. Also worked around it by renaming the field, but I think it's impact is higher than a low - given that it's not immediately obvious that this is the way to fix the problem.

Attached a project which replicates the error (limited to the binary search code).
Binary search cannot find "Owners" prefix, when the result set contains "Owners[0].Id".

imranbaloch wrote Dec 8, 2012 at 2:13 PM

ErikMarkland wrote Jan 8, 2013 at 4:21 PM

We also ran into this exact problem. This should have an impact much higher than "low".

Maxild wrote Mar 13, 2013 at 4:25 PM

Here is a failing test
public void Bug()
    var sut = new PrefixContainer(new[] { "SomeData", "ValueIsSequence", "Value[0].Data" });
I hate it that PrefixContainer is internal. Why? If it is good enough for Mvc why isn't it good enough for me? Also PrefixContainer has not tests at all:-(

Maxild wrote Mar 13, 2013 at 4:46 PM

Okay found the tests:-) Sry...for that naive comment

beauXjames wrote May 9, 2013 at 4:33 PM

...the object type we were having dumped was an array of long. This was nuts since it was only happening some times...the custom bindings were overkill and the any solutions we found were workarounds. The data was definitely getting corrupted between the js and the code...our models were right. The fact that it actually starts working when we changed the name of our property is insane. Will it break again later?

vshmidt wrote May 10, 2013 at 7:26 PM

also worked for me and also worked around it by renaming the field

yishaigalatzer wrote May 13, 2013 at 9:02 PM

Thanks for reporting the issue.

This was fixed on 4/22. And should be available in the next preview release, and currently is available in the nightly builds.

MichaConrad wrote Feb 6, 2015 at 11:57 AM

Hey guys, I know that's an old issue but we just had one team working on an MVC 4 project and they were running into that thing again. I tought it was fixed?
After diffing into the code of the latest MVC 4 release I found that the fix has NOT been applied to the MVC 4 branch. It has been fixed only in MVC 5.
Why is that? -.-

Here is the related code I'm talking about:
its still not doing the check the MVC 5 version is doing...
return Array.BinarySearch(_sortedValues, prefix, new PrefixComparer(prefix)) > -1;

yishaigalatzer wrote Feb 7, 2015 at 3:52 PM

Because we never port fixes back. Mvc 5 is fully backward compatible with mvc 4. You should upgrade.