0

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.

Marek
  • 261
  • 4
  • 14
  • You have to switch to a for loop in order for the model binding to work. See http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx. – Jason Berkan May 24 '13 at 19:35
  • But then, how can i access items in the list? I cant use "modelitem => item.Toode.Value" anymore. And seems like i cant use m=> m[i].Toode.Value either. – Marek May 24 '13 at 19:59

3 Answers3

1

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)
}
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • Allright, ill try! Thanks. But then, how can i access items in the list? I cant use "modelitem => item.Toode.Value" anymore. And seems like i cant use m=> m[i].Toode.Value either. – Marek May 24 '13 at 19:48
  • Do i have to value all propertys with html.hidden for in for loop to get that list to controller? – Marek May 25 '13 at 17:15
0

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

Softtech
  • 112
  • 6
  • I recive all the information in the view and i can show it to user aswell. But i need to pass all that information from that view to another contreoller. – Marek May 25 '13 at 17:13
  • you should use Any() instead of checking Count() > 0 – Bryan Oct 16 '13 at 17:31
0

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

Softtech
  • 112
  • 6
  • Please post your controller from where you get a model and controller where you want to pass this a model – Softtech May 25 '13 at 19:48