Reputation: 261
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">
}
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.
Upvotes: 0
Views: 2918
Reputation: 112
Ok , i understand.
Please try to convert your model data into IEnumerator<WebWareApp.ViewModels.OstukorvVM>
type.
IEnumerator<WebWareApp.ViewModels.OstukorvVM> ienut = model.GetEnumerator();
While(ienut.MoveNext())
{
///what ever you want to do
console.WriteLine(ienumerator.Current.ToString());
}
after that you still can get all values into model, this time is not going to null anymore. please send feed back
Upvotes: 0
Reputation: 112
As I understand your issue, you mean you are getting @model IEnumerable is null right?
then make sure when you return your list from your controller before that check for null and then return like this
if (db.OstukorvVMs != null && db.OstukorvVMs.Count() > 0)
{
return View(db.OstukorvVMs.ToList());
}
return View();
let me know your feedback if it will work
Upvotes: 0
Reputation: 60503
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)
}
Upvotes: 1