簡體   English   中英

ASP.NET MVC 中的下拉列表

[英]Drop-down list in 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>

在第二個 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();
    }

問題出在您的第二個 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替換為以下內容:

@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" })

嘗試這個:

 // 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