我在CSHTML中创建了一个表,我想将nr个项目== aantal的数组传递回我的控制器,但这似乎不起作用。任何想法是错的或为什么我在我的控制器中得到一个空引用?
CSHTML
@using (Html.BeginForm("OrderConfirm", "Beurs", new { vm = Model.Aantal }, method: FormMethod.Post))
{
<table class="table table-striped table-condensed table-bordered">
<tr>
<th>
Naam
</th>
<th>
Prijs
</th>
<th>
Minimum prijs
</th>
<th>
Factor
</th>
<th> Actie</th>
<!--
<th>Edit</th>-->
</tr>
@foreach (var item in Model.ItemLijstVm)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Naam)
</td>
<td>
€ @Html.DisplayFor(modelItem => item.Prijs)
</td>
<td>
€ @Html.DisplayFor(modelItem => item.MinimumPrijs)
</td>
<td>
@Html.DisplayFor(modelItem => item.Factor)
</td>
<td>
@Html.TextBoxFor(m => m.Aantal[item.Id - 1], new {type = "number" })
</td>
</tr>
}
</table>
<input type="submit" value=" Bevestig bestelling " width="120" />
}
视图模型
public class BeursLijstViewModel
{
public IEnumerable<BeursItemViewModel> ItemLijstVm{get; set;}
public string Naam { get; set; }
public double Crash { get; set; }
//References naar animated gif
public bool Event { get; set; }
public string GifPath { get; set; }
public int[] Aantal { get; set; }
public int VerhoogAllePrijzen { get; set; }
public double Totaal { get; set; }
public SelectListItem Categorie { get; set; }
public BeursLijstViewModel(Beurs beurs)
{
ItemLijstVm= beurs.Items.Select(g => new BeursItemViewModel(g));
Naam = beurs.Naam;
Aantal = new int[beurs.Items.Count()];
Totaal = beurs.Totaal;
}
}
控制器
[HttpPost]
public ActionResult OrderConfirm(int[] vm) //VM is null but should be array
{
//Some more code
}
我从模型中获取的帖子的引用为null,但如果我在我的foreach循环中声明它,它就可以了。我真的不知道出了什么问题:
@using (Html.BeginForm("Add", "Beurs", new { id = item.Id, aantal = Model.Aantal }, method: FormMethod.Post))
{
@Html.TextBoxFor(m => m.Aantal[item.Id - 1], new { type = "number" })
<input type="submit" value=" + " width="120"/>
}
答案 0 :(得分:0)
我认为你必须传回实际的模型,而不仅仅是希望获得结果的数组,因为当它绑定它时会有几个层,例如name =&#39; Model.item [z]&#39;
{{1}}
答案 1 :(得分:0)
@using (Html.BeginForm("OrderConfirm", "Beurs", new { vm = Model.Aantal }, method: FormMethod.Post))
changing the above part in combination with the answer of @COLD TOLD to
@using (Html.BeginForm("OrderConfirm", "Beurs"))
fixed my problem. Thanks for the help!