[英]How can I pass multiple values of a table/array to my controller
I create a table in my CSHTML, I want to pass the array of nr of items == aantal back to my controller however this doesn't seem to work. 我在CSHTML中创建了一个表,我想将nr项== aantal的数组传递回我的控制器,但这似乎不起作用。 Any idea whats wrong or why I get a null reference in my controller?
任何想法有什么问题,或者为什么我在控制器中获得空引用?
CSHTML 网页HTML
@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" />
}
ViewModel 视图模型
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;
}
}
Controller 控制者
[HttpPost]
public ActionResult OrderConfirm(int[] vm) //VM is null but should be array
{
//Some more code
}
The reference I get on my post from my model is null, but if i declare it in my foreach loop like this, it works. 我在模型中发布的引用为空,但是如果我在这样的foreach循环中声明该引用,它将起作用。 I really don't have a clue what goes wrong:
我真的不知道出什么问题了:
@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"/>
}
I think you have to pass back the actual model not just array expecting to get result, since when it binds it goes several layers like name='Model.item[z]' 我认为您必须传回实际模型,而不仅是希望得到结果的数组,因为绑定时它会经过几层,例如name ='Model.item [z]'
[HttpPost]
public ActionResult OrderConfirm(BeursLijstViewModel vm)
{
foreach (var item in vm.ItemLijstVm)
{
var response= vm.Aantal[item.Id - 1]
}
}
@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 结合@COLD TOLD的答案将上述部分更改为
@using (Html.BeginForm("OrderConfirm", "Beurs"))
fixed my problem. 解决了我的问题。 Thanks for the help!
谢谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.