ASP.NET MVC 中的下拉列表

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

在此输入图像描述

而另一个是空的

我想要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>


当年话下
浏览 114回答 3
3回答

函数式编程

在第 2 个 foreach 循环中,您将添加到第 1 个列表中,而不是名为 list1 的第 2 个列表。更正如下:&nbsp; &nbsp; public ActionResult Create()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var Sections = _context.Sections.ToList();&nbsp; &nbsp; &nbsp; &nbsp; var Customers = _context.Customers.ToList();&nbsp; &nbsp; &nbsp; &nbsp; List<SelectListItem> list1 = new List<SelectListItem>();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in Customers)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list1.Add(new SelectListItem { Text = item.Customer_Name, Value = item.Customer_Id.ToString() });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ViewBag.Customers = list1;&nbsp; &nbsp; &nbsp; &nbsp; List<SelectListItem> list2 = new List<SelectListItem>();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in Sections)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list2.Add(new SelectListItem { Text = item.Section_Name, Value = item.Section_Id.ToString() });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ViewBag.Sections = list2;&nbsp; &nbsp; &nbsp; &nbsp; return View();&nbsp; &nbsp; }

江户川乱折腾

问题在于您的第二个 for-each 循环,您将其添加到实际上面向客户的第一个列表中。SelectListItem此外,简化您的 GET 方法,如下所示。它将降低复杂性。Create// GET: Contracts/Create[HtttpGet]public ActionResult Create(){&nbsp; &nbsp; var sections = _context.Sections.ToList();&nbsp; &nbsp; var customers = _context.Customers.ToList();&nbsp; &nbsp; ViewBag.SectionSelectList = new SelectList(sections,"Section_Id","Section_Name");&nbsp; &nbsp; ViewBag.CustomerSelectList = new SelectList(customers,"Customer_Id","Customer_Name");&nbsp; &nbsp; return View();&nbsp;}然后在视图中将 s 替换为以下内容:@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" })

撒科打诨

试试这个:&nbsp;// GET: Contracts/Create&nbsp; &nbsp; &nbsp; &nbsp; public ActionResult Create()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;var listOfCustomers = new List<SelectListItem>();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in&nbsp; _context.Customers.ToList())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfCustomers.Add(new SelectListItem { Text = item.Customer_Name, Value = item.Customer_Id.ToString() });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ViewBag.Customers = listOfCustomers;&nbsp; &nbsp; &nbsp; &nbsp; var listOfSections = new List<SelectListItem>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in&nbsp; _context.Sections.ToList())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfSections.Add(new SelectListItem { Text = item.Section_Name, Value = item.Section_Id.ToString() });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ViewBag.Sections = listOfSections;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return View();&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP