如何通过多选列表的选择项背到控制器? [英] How to pass multiselect list's selected Items back to controller?

查看:144
本文介绍了如何通过多选列表的选择项背到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个MVC应用程序,其中包含了多选下拉列表。
我想下拉的多项选择的ID的。

I am developing a MVC application which contains the multiselect dropdown list. I want to get the ID's of multiple selected items of the drop down.

我在模型中的code

I have the code in model

namespace CustomerDEMOForMultiselect.Models
{
    public class Customer
    {
        private int _ID;
        private string _Name;
        private double _Amt;
        public int ID { get { return _ID; } set { _ID = value; }  }
        public string Name { get { return _Name; } set { _Name = value ; } }
        public double Amt { get { return _Amt; } set { _Amt = value; } }
    }
}

和控制器code是

namespace CustomerDEMOForMultiselect.Controllers
{
    public class CustomerController : Controller
    {
        public ActionResult DisplayCustomer()
        {
            Customer oCustomer = new Customer();
            List<Customer> CustomersList = new List<Customer>();
            CustomersList.Add(new Customer() { ID = 1, Name = "TestCustomer1", Amt = 123 });
            CustomersList.Add(new Customer() { ID = 2, Name = "TestCustomer2", Amt = 234 });
            CustomersList.Add(new Customer() { ID = 3, Name = "TestCustomer3", Amt = 324 });
            ViewBag.CustList = CustomersList;
            return View(CustomersList);
          }
    }
}

我没有得到什么在View写的,我已经尝试了不同的code,但我越来越混乱...

I am not getting what to write in View, I have tried different code but I am getting confusing...

code在View:

Code in View:

@model CustomerDEMOForMultiselect.Models.Customer 
@{
    Layout = null;
} 
<!DOCTYPE html>
<html>
    <head>
         <title>DisplayCustomer</title> 
    </head> 
    <body>
        <div>
             @using (Html.BeginForm())
             {
                 @Html.DropDownListFor(v => v.ID, new MultiSelectList(ViewBag.CustList,"ID","Name",ViewBag.ID)) 
                 <br /> 
                 <input type="submit" value="Submit" />
             } 
        </div> 
    </body> 
</html>

我要显示在查看客户名称列表,这样我就可以选择多个客户名称,并通过这些选定的客户ID的回控制器。
那怎么办?

I want to show the CustomerName list in View , so I can select multiple customer name and pass those selected customer ID's back to controller. How to do that?

推荐答案

使用包装模型的属性来选择客户绑定工作(我试了一下):

Using a wrapper model with a property to bind the selected customers to works (I tried it):

包装型号:

public class CustomerList
{
    public List<Customer> Customers { get; set; }
    public List<int> SelectedIDs { get; set; }
}

控制器:

        [HttpGet]
        public ActionResult DisplayCustomer()
        {
            Customer oCustomer = new Customer();
            List<Customer> CustomersList = new List<Customer>();
            CustomersList.Add(new Customer() { ID = 1, Name = "TestCustomer1", Amt = 123 });
            CustomersList.Add(new Customer() { ID = 2, Name = "TestCustomer2", Amt = 234 });
            CustomersList.Add(new Customer() { ID = 3, Name = "TestCustomer3", Amt = 324 });
            ViewBag.CustList = CustomersList;
            return View(new CustomerList() { Customers = CustomersList }); 

        }

        [HttpPost]
        public void DisplayCustomer(List<int> selectedIds)
        {
            // do something with the id list
        }

查看:

@model MvcApplication2.Models.CustomerList

@using (Html.BeginForm(@Model.SelectedIDs))
{
    @Html.ListBoxFor(m => m.SelectedIDs, new MultiSelectList(@Model.Customers, "ID", "Name", @Model.SelectedIDs))
    <input type="submit" value="save" />
}

您需要的东西,您的选择结合并发送回控制器。

You need something to bind your selection to and send it back to the controller.

这篇关于如何通过多选列表的选择项背到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆