3

WebGrid 列にドロップダウン リストを配置する方法を教えてください。

ドロップダウン リストをモデルのリストにバインドする必要があります。また、グリッド内の対応するすべてのレコードのリストからアイテムを選択する必要があります。

そして、selcetd 値をコントローラーにポストできるはずです。

mvc3 のいくつかのリンクDropdownlist propbelmといくつかの他のリンクで解決策を試しました。

サンプルコードを貼り付けています。

モデル:

public class AllocateToStore
    {
        public IList<OrderLine> FailureAllocations { get; set; }
        public IList<SelectListItem> AllocationStatus
        {
            get
            {
                // code to fetch list.
            }
        }
    }

 public class OrderLine
    {
        public long Id { get; set; }
        public DateTime Date { get; set; }
        public int Status { get; set; }
    }

コントローラ:

public ActionResult AutoAllocate()
        {
// This action will load the view with data.
// Get model data and send it to view.

            return View("Allocated",model);
        }

        [HttpPost]
        public ActionResult ResolveUnallocatedOrders(AllocateToStore coll)
        {
// When user changes the selection in grid and post the page I need to get the selection // here. So that I can update that record.
            return null;
        }

そしてビューは

@model AllocateToStore
@{
    ViewBag.Title = "Orders";
}
@{
    var grid = new WebGrid(Model.FailureAllocations, rowsPerPage: 100);
}

@using (Html.BeginForm("ResolveUnallocatedOrders", "OrderProcessing", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    if (Model.FailureAllocations.Any())
    {

    <div>
        @grid.GetHtml(
                columns: grid.Columns(
                    grid.Column(columnName: "Order date", header: "Order Date", format: item => item.Order.Date),
                    grid.Column("dropdown", header: "Resolution", format:
                                                            @<span>
                                                                @{ var index = Guid.NewGuid().ToString(); }
                                                                @Html.Hidden("Status.Index", index)
                                                                @Html.DropDownList("Status[" + index + "].SelectedValue", Model.AllocationStatus)
                                                            </span>
                    )
                ),
                tableStyle: "expandable-table",
                htmlAttributes: new { id = "gridFailureAllocations" }
            )
        <br />
        <input type="submit" value="Resolve" id="resolve-button" />
    </div>
    }

}

ありがとう、ナレシュ

4

1 に答える 1

3

以下が機能するはずです。

<div>
    @grid.GetHtml(
            columns: grid.Columns(
                grid.Column(columnName: "Date", header: "Order Date"),
                grid.Column("dropdown", header: "Resolution", format:
                    @<span>
                        @{ var index = Guid.NewGuid().ToString(); }
                        @Html.Hidden("FailureAllocations.Index", index)
                        @Html.Hidden("FailureAllocations[" + index + "].Id", (long)item.Id)
                        @Html.DropDownList("FailureAllocations[" + index + "].Status", Model.AllocationStatus)
                    </span>
                )
            ),
            tableStyle: "expandable-table",
            htmlAttributes: new { id = "gridFailureAllocations" }
        )
    <br />
    <input type="submit" value="Resolve" id="resolve-button" />
</div>

次に、コントローラーアクション内で、対応する値を取得するためにResolveUnallocatedOrders使用できます。coll.FailureAllocations

[HttpPost]
public ActionResult ResolveUnallocatedOrders(AllocateToStore coll)
{
    // coll.FailureAllocations will contain the necessary data
    ...
}

注:POSTアクションで値を取得する場合は、Order.Dateフィールドに追加の非表示フィールドを含めることができます。

于 2012-10-24T12:50:20.093 に答える