MVC传递IEnumerable< CustomVM>到控制器 [英] MVC passing IEnumerable&lt;CustomVM&gt; to Controller
本文介绍了MVC传递IEnumerable< CustomVM>到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我解决此问题吗?我想将自定义viewmodel的ienumerable或只是简单列表传递给控制器.有我的代码:
Can someone help me to solve this problem. I want to pass ienumerable or just simple list of my custom viewmodel to controller. Theres my code:
@model IEnumerable<WebWareApp.ViewModels.OstukorvVM>
@{
ViewBag.Title = "Ostukorv";
}
<h2>Hetkel ostukorvis olevad tooted:</h2>
@using (Html.BeginForm("Create", "ToodeTellimuses"))
{ @Html.AntiForgeryToken()
<table>
<tr>
<th>Tootenimi</th>
<th>Tootegrupp</th>
<th>Tootja</th>
<th>Mõõdud</th>
<th>Varvus</th>
<th>Hind</th>
<th>Vali Kogus</th>
<th></th>
</tr>
@{if (Model != null)
{
foreach (var item in Model)
{<tr>
<td>
@Html.DisplayFor(modelitem => item.Toode.Nimi)
@Html.HiddenFor(modelitem => item.Toode.Nimi)
</td>
<td>
@Html.DisplayFor(modelitem => item.Toode.Tootegrupp.Nimetus)
@Html.HiddenFor(modelitem => item.Toode.Tootegrupp.Nimetus)
</td>
<td>
@Html.DisplayFor(modelitem => item.Toode.Tootja)
@Html.HiddenFor(modelitem => item.Toode.Tootja)
</td>
<td>
@Html.DisplayFor(modelitem => item.Toode.Pikkus) x @Html.DisplayFor(modelitem => item.Toode.Laius) x @Html.DisplayFor(modelitem => item.Toode.Korgus)
@Html.HiddenFor(modelitem => item.Toode.Pikkus) @Html.HiddenFor(modelitem => item.Toode.Laius) @Html.HiddenFor(modelitem => item.Toode.Korgus)
</td>
<td>
@Html.DisplayFor(modelitem => item.Toode.Varvus)
@Html.HiddenFor(modelitem => item.Toode.Varvus)
</td>
<td>
@Html.DisplayFor(modelitem => item.Toode.Hind)
@Html.HiddenFor(modelitem => item.Toode.Hind)
</td>
<td>
@Html.EditorFor(modelitem => item.kogus)
</td>
<td>
@String.Format("Laos olemas {0}",item.Toode.Kogus)
@Html.HiddenFor(modelitem => item.Toode.Kogus)
</td>
</tr>
}
}}
</table>
<input type="submit" value="Telli">
}
但是问题是当我调试控制器时,传入的模型字段始终为null.如有必要,我也可以将我的viewmodel和模型以及控制器发布到此处.
But the problem is that in my controller when i debugg the incoming model fields are always null.If necessary i can post my viewmodel and models and controller to here also.
推荐答案
用 for
循环替换 foreach
循环即可解决问题.
Replacing the foreach
loop with a for
loop will do the trick.
for (var i = 0; i < Model.Count; i++) {
@Html.HiddenFor(m => Model[i].Toode.Value)
}
这篇关于MVC传递IEnumerable< CustomVM>到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文