asp.net-mvc-3 - Webgrid 中的下拉列表

标签 asp.net-mvc-3 asp.net-mvc-4 webgrid html.dropdownlistfor html-select

有人可以帮助我如何在我的 webgrid 列中放置一个下拉列表。

我需要将我的下拉列表绑定(bind)到模型中的列表。并且需要为网格中的每条对应记录从列表中选择一个项目。

而且我应该能够将 selcetd 值发布到 Controller 。

我已经在几个链接中尝试了给出的解决方案 Dropdownlist propbelm in mvc3和其他一些人。

我正在粘贴我的示例代码。

型号:

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; }
    }

Controller :

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;
        }

View 是

@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>
    }

}

谢谢, 纳什

最佳答案

以下应该有效:

<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 Controller 操作中,您可以使用 coll.FailureAllocations 来检索相应的值:

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

注意:如果您想在 POST 操作中取回它的值,您可以为 Order.Date 字段包含一个额外的隐藏字段。

关于asp.net-mvc-3 - Webgrid 中的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018632/

相关文章:

c# - 使用 mvc C# 和 sql 的 highcharts

asp.net-mvc - MVC WebGrid 设置呈现的ID

jquery - 将鼠标悬停在 MVC3 Razor WebGrid 行上时显示包含更多信息的工具提示

jquery - 使用 Jquery 提交表单,提交 Get 而不是 Post

asp.net-mvc - Azure 网站中的 404 处理

asp.net-mvc-3 - 验证AntiForgeryToken和授权超时

asp.net-mvc-3 - MVCSerializer.Deserialize() 在哪里?

c# - T4MVC 何时会支持带有显式 HtmlHelpers 的 Razor 来渲染局部?

asp.net-mvc - 在出现特定操作时使用 MVC 路由覆盖 Controller 名称

asp.net-mvc - asp mvc 使用 View 模型在 View 中列出产品详细信息