如何将模型中每个项目的值从视图发布到控制器作为列表或数组?

时间:2018-05-21 15:24:12

标签: asp.net asp.net-mvc asp.net-mvc-5

example of view priority list of orders 我有一个订单模型,我需要用户按数字顺序排列订单。 为每个订单提供不同数量的优先级,并向控制器发送订单的ID和他为订单选择的优先级编号。 将它作为对的列表/数组(id,position)发布到控制器。

以及如何在actionResult中接收值对的列表/数组。

这是我的PackerController:

    public ActionResult SetByCity(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var supplier = db.Suppliers.Where(s => s.Id == id).FirstOrDefault();

        var mySupplierOrders = db.Orders.Where(o => o.SupplierId == supplier.Id && o.SupplierApproval == 1).Include(o => o.Clients).Include(o => o.Suppliers);
        return View(mySupplierOrders.OrderBy(o => o.Clients.BusinessAddress).ToList());           
    }

这是“SetByCity”的视图:

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Clients.BusinessName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Clients.BusinessAddress)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CreateDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PayDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Discount)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TotalPrice)
            </td>
            <td>
                <form method="post" action="@Url.Action("SetOrdersPosition", "Packer")" id="editform">
                    <input type="hidden" name="Id" value="@item.Id">
                    <input type="number" name="Position" min="1" max="@Model.Count()">@*i need to put all the positions and id's of all the items in a pairs list and send it to the controller*@
                </form>
            </td>
        </tr>
    }
</table>
<input type="submit" value="send" form="editform" />

这是在PackerController中接收actionResult“SetOrdersPosition”:

    [HttpPost]
    public ActionResult SetOrdersPosition(List<id,position>)
    {
        //does something...
    }

我不知道在SetOrdersPosition获取的参数中放入什么......

1 个答案:

答案 0 :(得分:0)

希望您使用名为&#34;供应商&#34;的型号名称。 所以在get方法中将其传递到视图中。 您需要在视图中使用Begin Form进行一些相应的更改,以便您可以直接将模型数据发布到控制器。 在体内使用此项目。 别忘了在视图页面中声明您正在使用的模型,如下所示。 @model MVC.Models.Suppliers

@using (Html.BeginForm("SetOrdersPosition", "Packer", FormMethod.Post))
    {
      <table>
        @foreach (var item in Model)
           {
     <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Clients.BusinessName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Clients.BusinessAddress)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CreateDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PayDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Discount)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TotalPrice)
        </td>
        </tr>
         <tr>
            <td><input type="submit" value="Submit"/></td>
      </tr>
     }
     </table>
}

控制器代码[HttpPost]

 public ActionResult SetOrdersPosition(Suppliers _suppliers)
  {
    //does something...
  }