简体   繁体   English

ASP.NET MVC 中的下拉列表

[英]Drop-down list in ASP.NET MVC

I want to create two drop-down lists, but the data come on one drop-down list like this picture :我想创建两个下拉列表,但数据出现在一个下拉列表中,如下图:

enter image description here在此处输入图片说明

and the other one is empty而另一个是空的

i want the B1 and B2 in one drop-down list我想要一个下拉列表中的 B1 和 B2

and name zahra in other drop-down list并在其他下拉列表中命名 zahra

This code in the controller :控制器中的这段代码:

// 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();
}

And this is in the 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>

In the 2nd foreach loop you are adding in the 1st list instead of 2nd list named as list1.The corrected ones given below:在第二个 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();
    }

Problem is in your second for-each loop where you are adding SelectListItem to the first list that is actually for customer.问题出在您的第二个 for-each 循环中,您将SelectListItem添加到实际供客户使用的第一个列表中。

Moreover Simplify your Create GET method as follows.此外,请按如下方式简化您的Create GET 方法。 It will reduce the complexity.它会降低复杂性。

// 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();
 }

Then in the view replace your @Html.DropDownListFor s with the followings:然后在视图@Html.DropDownListFor您的@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" })

Try this:尝试这个:

 // 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();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM