3

有人能帮助我如何将下拉列表放入我的webgrid列中。Webgrid中的下拉列表

我需要将我的下拉列表绑定到Model中的列表。并且需要从列表中为网格中的每个相关记录选择一个项目。

而且我应该能够将selcetd值发布到控制器。

我曾尝试解决方案givine在几个链接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; } 
    } 

控制器:

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

} 

感谢, 纳雷什

回答

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

备注:如果你愿意,你可以包括对Order.Date领域的额外隐藏字段在POST操作中恢复其值。

+0

感谢您的回复Darin。但是我的下拉列表没有在页面加载时被选中。我怎样才能做到这一点?谢谢。 – Naresh

+1

我已经解决了这个问题,如@ Html.DropDownList(“FailureAllocations [”+ index +“] .Status”,新的SelectList(Model.AllocationStatus,“Value”,“Text”,item.Status))。如果我错了,请纠正我。谢谢。 – Naresh

+0

感谢Darin和Naresh ..结合两者都很好...... – Habeeb