ASP NET MVC 4集是空后 [英] ASP NET MVC 4 collection is null on post
问题描述
我读书最多的谷歌:-),但我无法进行。我的对象上的收集,并保持零上后,无论我做什么。
I read most of Google :-), but I can't proceed. The collection on my object is and stays null on post, whatever I do.
我的模型:
public class ArticleViewModel
{
public Guid EventId { get; set; }
public IList<ArticleItemViewModel> ArtikelListe { get; set; }
public decimal GesamtpreisNetto { get; set; }
public decimal MwSt { get; set; }
}
和
public class ArticleItemViewModel
{
public Guid EventId { get; set; }
public Guid Id { get; set; }
public string Artikelname { get; set; }
public string Artikelname_EN { get; set; }
public string Information { get; set; }
public string Information_EN { get; set; }
public decimal Preis { get; set; }
public bool MitAnzahl { get; set; }
public bool IstKategorie { get; set; }
public int Anzahl { get; set; }
public bool Checkbox { get; set; }
public int Reihenfolge { get; set; }
}
我的观点:
@using (Html.BeginForm("Next", "Article", FormMethod.Post))
{
@Html.HiddenFor(x => x.EventId)
<input type="hidden" name="ArtikelListe" />
for (var i = 0; i < Model.ArtikelListe.Count; i++)
// foreach (EventManager.ViewModels.ArticleItemViewModel artikelItem in Model.ArtikelListe)
{
<div>
<div>
@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" })
}
else
{
@Html.LabelFor(x => x.ArtikelListe[i].Anzahl)
}
@Html.LabelFor(x => x.ArtikelListe[i].Artikelname)<br />
@Html.LabelFor(x => x.ArtikelListe[i].Information)
}
</div>
</div>
}
在文章中,我得到我的ViewModel回来了,它具有ArtikelListe的收集与15个项目(这就是正确的),但这些都是空!
On post, I get my Viewmodel back and it has a Collection of ArtikelListe with 15 items (thats correct), but these are all null!
在我的HTTP头,我得到以下数据后:
In my HTTP Header I get the following post data:
EventId:824e7f3c-7190-4ebb-aa60-51b57c977b1e
ArtikelListe:
ArtikelListe[1].Anzahl:0
ArtikelListe[2].Anzahl:1
ArtikelListe[3].Anzahl:0
submitButton:Nächste
我不知道为什么只是部分数据被withon的HTTP POST发送,为什么我的所有列表项都为空。我试图为和foreach渲染通过。同样的结果。
I wonder why just partial data is sent withon the http post and why all my list items are null. I tried to render by for and foreach. same result.
任何想法?我很无奈。
推荐答案
集合索引必须从零开始,并是连续的(除非你包括首页
属性)。由于你的如果
语句,你并不一定产生控制的财产 Anzahl
。看着你的头信息,你不必为 ArtikelListe [0] .Anzahl
的值,这意味着第一个项目必须具有 IstKategorie = TRUE
或 MitAnzahl = FALSE
。您可以通过添加一个隐藏的输入,所以值回发更正此
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)
}
另外,您可以添加该 DefaultModelBinder
用来匹配是不连续的收藏项的首页
属性
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.
请注意为谢尔盖说,你也需要删除&LT;输入类型=隐藏的名字=ArtikelListe/&GT;
Note as Sergey noted, you need to also remove <input type="hidden" name="ArtikelListe" />
这篇关于ASP NET MVC 4集是空后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!