Asp.net mvc 在ASP.NET MVC中获取服务器端的多选值

Asp.net mvc 在ASP.NET MVC中获取服务器端的多选值,asp.net-mvc,get,server-side,multi-select,selectedvalue,Asp.net Mvc,Get,Server Side,Multi Select,Selectedvalue,我有下表PurchaseQuery,供应商。但是PurchaseQuery可以有多个供应商,因此添加了第三个表PurchaseQuerySupplier以保留两个表的ID。 我在PurchaseQuery表单中添加了一个多选列表来选择多个供应商 @Html.ListBoxFor(model => model.PurchaseQuerySuppliers, new MultiSelectList(Suppliers.AsEnumera

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

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

                            @Html.ListBoxFor(model => model.PurchaseQuerySuppliers, new MultiSelectList(Suppliers.AsEnumerable(), "ID", "Name"), new { @class = "chosen-select"})
但在我的动作控制器中,我得到PurchaseQuerySuppliers的空对象。虽然我可以在FormCollection中使用逗号分隔的供应商值,但我希望将PurchaseQuerySuppliers作为操作控制器中的PurchaseQuery中的对象。
有什么想法吗?

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

public class PurchaseSupplierViewModel
{
    public List<int> SelectedSupplies { get; set; } 
    public List<SelectListItem> Suppliers { get; set; } 
}
公共类PurchaseSupplierViewModel
{
公共列表SelectedSupplies{get;set;}
公共列表供应商{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
}
public ActionResult YourAction()
{
列出供应商列表=(从DB获取所有供应商)
var模型=新的PurchaseSupplierViewModel()
{
供应商=供应商列表。选择(x=>new SelectListItem
{
Value=x.ID.ToString(),
Text=x.名称,
})托利斯先生()
};
返回视图(模型);
}
[HttpPost]
公共行动结果您的行动(PurchaseSupplierViewModel)
{
//model.SelectedSupplies将在此处包含选定的供应商ID
}
然后是一种观点:

@model PurchaseSupplierViewModel

@using (Html.BeginForm())
{
    @Html.ListBoxFor(x => x.SelectedSupplies, Model.Suppliers, new { @class = "chosen-select" })
    <input type="submit" value="Submit"/>
}
@model采购商供应商视图模型
@使用(Html.BeginForm())
{
@Html.ListBoxFor(x=>x.SelectedSupplies,Model.Suppliers,new{@class=“selected select”})
}

以下是我将如何通过一些测试示例来实现这一点:

型号:

    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; }
    }
公共类供应商模型
{
公共列表供应商{get;set;}
公共字符串[]PurchaseQuerySuppliers{get;set;}
}
公共类供应商
{
公共int ID{get;set;}
公共字符串名称{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);
    }
公共操作结果列表测试()
{
var模型=新供应商模型();
model.Suppliers=新列表();
model.Suppliers.Add(新供应商{ID=1,Name=“Name1”});
model.Suppliers.Add(新供应商{ID=2,Name=“Name2”});
返回视图(模型);
}
[HttpPost]
公共行动结果列表测试(供应商模型)
{
字符串[]selectedItems=model.PurchaseQuerySuppliers;
返回视图(模型);
}
视图:

@模型供应商模型
@使用(Html.BeginForm())
{
@Html.ListBoxFor(model=>model.PurchaseQuerySuppliers,新的MultiSelectList(model.Suppliers.AsEnumerable(),“ID”,“Name”),新的{@class=“selected select”})
}
@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" />
}