Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Post list becomes null under very strange circumstances
    primarykey
    data
    text
    <p>So I have a controller like this:</p> <pre><code>public class TestController : Controller { // // GET: /Test/ public ActionResult Index() { return View("Test"); } public ActionResult Post(IList&lt;Test&gt; LanguageStrings, IList&lt;Test&gt; LanguageStringsGroup, IList&lt;string&gt; Deleted, IList&lt;string&gt; DeletedGroup) { if (LanguageStrings == null) { throw new ApplicationException("NULL"); } return View("Test"); } } public class Test { public string Val { get; set; } public string Another { get; set; } } </code></pre> <p>And a view like this:</p> <pre><code>&lt;h2&gt;Test&lt;/h2&gt; @using (Html.BeginForm("Post", "Test")) { @Html.Hidden("LanguageStrings[0].Val", "test1") @Html.Hidden("LanguageStrings[0].Another") @Html.Hidden("LanguageStrings[1].Val", "test2") @Html.Hidden("LanguageStrings[1].Another") @Html.Hidden("LanguageStringsGroup[0].Val", "test4") @Html.Hidden("Deleted[0]") @Html.Hidden("Deleted[1]") @Html.Hidden("Deleted[2]") @Html.Hidden("DeletedGroup[0]") &lt;button&gt;Post&lt;/button&gt; } </code></pre> <p>When I post the form my controller throws the exception because LanguageStrings is null. The strange part I mentioned in the title is that if I add one more record to the list everything works. Like this:</p> <pre><code>&lt;h2&gt;Test&lt;/h2&gt; @using (Html.BeginForm("Post", "Test")) { @Html.Hidden("LanguageStrings[0].Val", "test1") @Html.Hidden("LanguageStrings[0].Another") @Html.Hidden("LanguageStrings[1].Val", "test2") @Html.Hidden("LanguageStrings[1].Another") @Html.Hidden("LanguageStrings[2].Val", "test3") @Html.Hidden("LanguageStrings[2].Another") @Html.Hidden("LanguageStringsGroup[0].Val", "test4") @Html.Hidden("Deleted[0]") @Html.Hidden("Deleted[1]") @Html.Hidden("Deleted[2]") @Html.Hidden("DeletedGroup[0]") &lt;button&gt;Post&lt;/button&gt; } </code></pre> <p>It also works when I remove the "Deleted" list. Like this: </p> <pre><code>&lt;h2&gt;Test&lt;/h2&gt; @using (Html.BeginForm("Post", "Test")) { @Html.Hidden("LanguageStrings[0].Val", "test1") @Html.Hidden("LanguageStrings[0].Another") @Html.Hidden("LanguageStrings[1].Val", "test2") @Html.Hidden("LanguageStrings[1].Another") @Html.Hidden("LanguageStringsGroup[0].Val", "test4") @Html.Hidden("DeletedGroup[0]") &lt;button&gt;Post&lt;/button&gt; } </code></pre> <p>This has something to do with the naming I am using. I have already solved the problem with renaming LanguageStrings to something else. But I would like to understand what is happening here because probably I could learn something from it how MVC maps request body and will be able to avoid similar time consuming problems. Please help me and explain the cause of this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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