ASP.NET MVC中的下拉列表

时间:2019-02-11 07:33:28

标签: c# asp.net-mvc

我想创建两个下拉列表,但是数据在一个下拉列表中,如下图所示:

enter image description here

另一个为空

我希望B1和B2在一个下拉列表中

并在其他下拉列表中命名zahra

此代码在控制器中:

// GET: Contracts/Create
public ActionResult Create()
{
    var Sections = _context.Sections.ToList();

    var Customers = _context.Customers.ToList();

    List<SelectListItem> list = new List<SelectListItem>();

    foreach (var item in Customers)
    {
            list.Add(new SelectListItem { Text = item.Customer_Name, Value = item.Customer_Id.ToString() });

            ViewBag.Customers = list;
    }


    List<SelectListItem> list1 = new List<SelectListItem>();

    foreach (var item in Sections)
    {
        list.Add(new SelectListItem { Text = item.Section_Name, Value = item.Section_Id.ToString() });

        ViewBag.Sections = list1;
    }


    return View();
}

这是在视图中

    <div class="form-group">
        @Html.LabelFor(model => model.CustomerId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.CustomerId, (IEnumerable<SelectListItem>)ViewBag.Customers, "Select customers", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CustomerId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SectionsId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownListFor(model => model.SectionsId, (IEnumerable<SelectListItem>)ViewBag.Sections, "Select Sections", new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.SectionsId, "", new { @class = "text-danger" })
        </div>
    </div>

3 个答案:

答案 0 :(得分:1)

问题在您的第二个for-each循环中,您在其中将SelectListItem添加到实际上是针对客户的第一个列表中。

此外,如下简化您的Create GET方法。它将降低复杂性。

// GET: Contracts/Create
[HtttpGet]
public ActionResult Create()
{
    var sections = _context.Sections.ToList();
    var customers = _context.Customers.ToList();

    ViewBag.SectionSelectList = new SelectList(sections,"Section_Id","Section_Name");
    ViewBag.CustomerSelectList = new SelectList(customers,"Customer_Id","Customer_Name");

    return View();
 }

然后在视图中将您的@Html.DropDownListFor替换为以下内容:

@Html.DropDownListFor(model => model.CustomerId, ViewBag.CustomerSelectList, "Select customers", new { @class = "form-control" })

@Html.DropDownListFor(model => model.SectionsId, ViewBag.SectionSelectList , "Select Sections", new { @class = "form-control" })

答案 1 :(得分:0)

在第二个foreach循环中,您将添加第一个列表而不是名为list1的第二个列表。下面给出了更正的列表:

    public ActionResult Create()
    {

        var Sections = _context.Sections.ToList();

        var Customers = _context.Customers.ToList();

        List<SelectListItem> list1 = new List<SelectListItem>();

        foreach (var item in Customers)
        {
            list1.Add(new SelectListItem { Text = item.Customer_Name, Value = item.Customer_Id.ToString() });
        }
        ViewBag.Customers = list1;


        List<SelectListItem> list2 = new List<SelectListItem>();

        foreach (var item in Sections)
        {
            list2.Add(new SelectListItem { Text = item.Section_Name, Value = item.Section_Id.ToString() });
        }
        ViewBag.Sections = list2;

        return View();
    }

答案 2 :(得分:-1)

尝试一下:

 // GET: Contracts/Create
        public ActionResult Create()
        {


       var listOfCustomers = new List<SelectListItem>();

        foreach (var item in  _context.Customers.ToList())
        {
            listOfCustomers.Add(new SelectListItem { Text = item.Customer_Name, Value = item.Customer_Id.ToString() });

        }

        ViewBag.Customers = listOfCustomers;

        var listOfSections = new List<SelectListItem>();

            foreach (var item in  _context.Sections.ToList())
            {
                listOfSections.Add(new SelectListItem { Text = item.Section_Name, Value = item.Section_Id.ToString() });

            }
        ViewBag.Sections = listOfSections;

            return View();
        }