亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ASP.NET MVC 中的下拉列表

ASP.NET MVC 中的下拉列表

C#
當年話下 2022-09-04 17:06:33
我想創建兩個下拉列表,但數據出現在一個下拉列表中,如下圖所示:在此輸入圖像描述而另一個是空的我想要B1和B2在一個下拉列表中并在其他下拉列表中命名 zahra控制器中的此代碼:// GET: Contracts/Createpublic 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 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

在第 2 個 foreach 循環中,您將添加到第 1 個列表中,而不是名為 list1 的第 2 個列表。更正如下:


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

    }


查看完整回答
反對 回復 2022-09-04
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

問題在于您的第二個 for-each 循環,您將其添加到實際上面向客戶的第一個列表中。SelectListItem


此外,簡化您的 GET 方法,如下所示。它將降低復雜性。Create


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

 }

然后在視圖中將 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" })



查看完整回答
反對 回復 2022-09-04
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

試試這個:


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

        }


查看完整回答
反對 回復 2022-09-04
  • 3 回答
  • 0 關注
  • 144 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號