The point is that the following model is passed to the view:

public class IndexViewModel { public IEnumerable<OrderViewModel> Orders { get; set; } public PageInfo PageInfo { get; set; } } 

In the view itself, the Orders collection is displayed in the foreach construction:

  @using (Html.BeginForm("OrdersAction", "Admin", FormMethod.Post)) { .......... @foreach (var o in Model.Orders) { <tr> <td>@Html.CheckBoxFor(m => o.Selected)</td> <td>@Html.TextBoxFor(m => o.Id)</td> <td>@Html.TextBoxFor(m => o.UserName)</td> .......... </tr> } <button name="action" value="confirm">Подтвердить</button><br> <button name="action" value="cancel">Отменить</button><br> ..... <div class="btn-group"> @Html.PageLinks(Model.PageInfo, x => Url.Action("Orders", new { page = x })) </div> } 

The view uses pagination (helper @Html.PageLinks() ), if that matters. I can not figure out how to get in the controller collection Orders .

  • In which controller? - skubarenko
  • one
    Which refers to the form Admin / OrdersAction - Andrew A
  • And how did you get it in the Orders method? Also get it in the admin controller. - skubarenko
  • [HttpPost] public ActionResult Save (IEnumerable <OrderViewModel> Orders) - that sent, then accept. - Andryxa

1 answer 1

Take a collection of data to convert to a model view, I understand? If so, then I encountered a similar problem and for a few hours puzzled, in the end, something like this turned out:

 <div class="row row-counter"> <input hidden type="text" name="adreses[@i].id" value="@adreses[i].id"> <div class="col-sm-5"> <input type="text" name="adreses[@i].key" class="form-control" value="@adreses[i].key" placeholder="Салон"> </div> <div class="col-sm-5"> <input type="text" name="adreses[@i].value" class="form-control" value="@adreses[i].value" placeholder="Салон"> </div> <div class="col-sm-2"> <div class="btn btn-danger col-sm-12 row-del">X</div> </div> </div> 

This is a clipping from the view, which should push you to the idea that the collection for a successful conversion to the View should look like "the name of the matching parameter with the name in the model [ index ]. Field ".

Directly for your example: model . Orders [ i ]. UserName , where model is the name of the parameter taken in the form handler:

 [HttpPost] public ActionResult EditAutosalon(IndexViewModel model) 
  • offtop: I do not understand, is it difficult to mark the answer with a solution? - gromanev