【问题标题】:Get multiselect values on the server side in ASP.NET MVC在 ASP.NET MVC 中的服务器端获取多选值
【发布时间】:2014-01-09 12:58:05
【问题描述】:

我有以下表格 PurchaseQuery、Suppliers。但是 PurchaseQuery 可以有多个供应商,因此添加了第三个表 PurchaseQuerySupplier 来保留两个表的 ID。

我有 PurchaseQuery 表单,我在其中添加了一个多选列表来选择多个供应商。

                            @Html.ListBoxFor(model => model.PurchaseQuerySuppliers, new MultiSelectList(Suppliers.AsEnumerable(), "ID", "Name"), new { @class = "chosen-select"})

但在我的动作控制器中,我得到了 PurchaseQuerySuppliers 的空对象。虽然我可以在 FormCollection 中用逗号分隔供应商值,但我想在 Action Controller 中获取 PurchaseQuerySuppliers 作为 PurchaseQuery 内的对象。 有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc get server-side multi-select selectedvalue


    【解决方案1】:

    你可以编写一个视图模型来解决这个问题。

    public class PurchaseSupplierViewModel
    {
        public List<int> SelectedSupplies { get; set; } 
        public List<SelectListItem> Suppliers { get; set; } 
    }
    

    和你的控制器:

    public ActionResult YourAction()
    {
        List<Supplier> SupplierList = (get all Suppliers from DB here) 
        var model = new PurchaseSupplierViewModel()
        {
           Suppliers = SupplierList.Select(x => new SelectListItem
                            {
                                Value = x.ID.ToString(),
                                Text = x.Name,
                            }).ToList()
        };
        return View(model);
    }
    
    [HttpPost]
    public ActionResult YourAction(PurchaseSupplierViewModel model)
    {
        // model.SelectedSupplies will contain the selected Supplier IDs here
    }
    

    然后是视图:

    @model PurchaseSupplierViewModel
    
    @using (Html.BeginForm())
    {
        @Html.ListBoxFor(x => x.SelectedSupplies, Model.Suppliers, new { @class = "chosen-select" })
        <input type="submit" value="Submit"/>
    }
    

    【讨论】:

      【解决方案2】:

      以下是我将如何使用一些测试示例:

      型号:

          public class SuppliersModel
          {
              public List<Supplier> Suppliers { get; set; }
              public string[] PurchaseQuerySuppliers { get; set; }
          }
      
          public class Supplier
          {
              public int ID { get; set; }
              public string Name { get; set; }
          }
      

      动作:

          public ActionResult ListTest()
          {
              var model = new SuppliersModel();
              model.Suppliers = new List<Supplier>();
              model.Suppliers.Add(new Supplier { ID = 1, Name = "Name1"});
              model.Suppliers.Add(new Supplier { ID = 2, Name = "Name2" });
              return View(model);
          }
      
          [HttpPost]
          public ActionResult ListTest(SuppliersModel model)
          {
              string[] selectedItems = model.PurchaseQuerySuppliers;
      
              return View(model);
          }
      

      查看:

      @model SuppliersModel
      @using (Html.BeginForm())
      {
      @Html.ListBoxFor(model => model.PurchaseQuerySuppliers, new MultiSelectList(Model.Suppliers.AsEnumerable(), "ID", "Name"), new { @class = "chosen-select"})
      <input type="submit" value="submit" />
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多