ASP NET MVC 4 collection is null on post

后端 未结 3 771
时光取名叫无心
时光取名叫无心 2020-12-22 13:06

I read most of Google :-), but I can\'t proceed. The collection on my object is and stays null on post, whatever I do.

My Model:

public class Artic         


        
相关标签:
3条回答
  • 2020-12-22 13:51

    Collection indexers must start at zero and be consecutive (unless you include an Index property).Because of your if statements, you are not necessarily generating a control for the property Anzahl. Looking at your header information, you do not have a value for ArtikelListe[0].Anzahl which means that the first item must have either IstKategorie=true or MitAnzahl=false. You can correct this by adding a hidden input so a value posts back

    @if (Model.ArtikelListe[i].IstKategorie)
    {
      @Html.LabelFor(x => x.ArtikelListe[i].Artikelname)<br />  
      @Html.LabelFor(x => x.ArtikelListe[i].Information)
      @Html.HiddenFor(x => x.ArtikelListe[i].Anzahl) // add this
    }
    else
    {
      if (Model.ArtikelListe[i].MitAnzahl)
      {
        @Html.TextBoxFor(x => x.ArtikelListe[i].Anzahl, new { @class = "field text fn" })
      }
      else
      {
        @Html.LabelFor(x => x.ArtikelListe[i].Anzahl)
        @Html.HiddenFor(x => x.ArtikelListe[i].Anzahl) // add this         
      }
      @Html.LabelFor(x => x.ArtikelListe[i].Artikelname)<br />  
      @Html.LabelFor(x => x.ArtikelListe[i].Information)
    }
    

    Alternatively you can add an Index property which the DefaultModelBinder uses to match up collection items that are non-consecutive

    @if (Model.ArtikelListe[i].IstKategorie)
    {
      @Html.LabelFor(x => x.ArtikelListe[i].Artikelname)<br />  
      @Html.LabelFor(x => x.ArtikelListe[i].Information)
    }
    else
    {
      if (Model.ArtikelListe[i].MitAnzahl)
      {
        @Html.TextBoxFor(x => x.ArtikelListe[i].Anzahl, new { @class = "field text fn" })
        <input type="hidden" name="x.ArtikelListe.Index" value="@i" /> // add this manually
      }
      else
      {
        @Html.LabelFor(x => x.ArtikelListe[i].Anzahl)     
      }
      @Html.LabelFor(x => x.ArtikelListe[i].Artikelname)<br />  
      @Html.LabelFor(x => x.ArtikelListe[i].Information)
    }
    

    With the first option, it will post back all items. In the second case it will post back only items that meet the if conditions.

    Note as Sergey noted, you need to also remove <input type="hidden" name="ArtikelListe" />

    0 讨论(0)
  • 2020-12-22 13:57

    The issue in this line:

    <input type="hidden" name="ArtikelListe" />
    

    When POST request is sent back:

    ArtikelListe:
    ArtikelListe[1].Anzahl:0
    ArtikelListe[2].Anzahl:1
    ArtikelListe[3].Anzahl:0
    

    Then ArtikelListe overrides value for the list and that's why it's always null. So you just need to rename your hidden field to some another name to not conflict with existing names.

    Here is working example based on your MVC code in DotNetFiddle - https://dotnetfiddle.net/BCXduq

    You can click RUN, then enter some values in two input fields in the right bottom box, and click Save button. And then it will display model that server received in POST as JSON text.

    0 讨论(0)
  • 2020-12-22 13:58

    Thank you guys.

    The problem was, that I did the testing with the script from the Fiddle. In this code

    Model.ArtikelListe[i].MitAnzahl
    

    was always true.

    In case it is not true, the value "Anzahl" was not bound to a control containing a value (but just a label).

    @Html.LabelFor(x => x.ArtikelListe[i].Anzahl)
    

    As soon as I inserted a hidden field within that scope and bound the "Anzahl" value to it, the post returned with all the data I expected.

    Thanks anyway. I learned a lot!

    0 讨论(0)
提交回复
热议问题