简体   繁体   English

我已经创建了一个 2 多选列表,它显示在一个文本框中,我将运行该程序

[英]I have created a 2 multiselect list and it is displaying in one single text box will I run the programme

I have created 2 multiselect list for Contact Group details and employee details in a create() function, while the view for both of them are different at runtime it appears in 1 textbox.. Not sure why is it happening?我在 create() 函数中为联系人组详细信息和员工详细信息创建了 2 个多选列表,而它们的视图在运行时不同,它出现在 1 个文本框中。不确定为什么会这样?

I tried changing the view, and the controller as well here is the code for my controller:我尝试更改视图,控制器也在这里是我的控制器的代码:

// GET: DistributionLists/Create

        public ActionResult Create()

        {   //LIST FOR CONTACT DETAILS

            var contact = db.Contact;

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

            foreach (var item in contact)

            {

              items.Add(new SelectListItem

            {

              Value = item.EmployeeId.ToString(),

                Text = item.EmployeeName.ToString(),

            });

 

            };

            ViewBag.Contacts = items;

            //  ViewBag.EmployeeId = new SelectList (db.Contact, "EmployeeId","EmployeeName");

 

            //LIST FOR CONTACT GROUP DETAILS

            var cg = db.ContactGroup;

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

            foreach (var im in cg)

            {

                items.Add(new SelectListItem

                {

                    Value = im.ContactGroupID.ToString(),

                    Text = im.ContactGroupName.ToString(),

                });

 

            };

            ViewBag.cg = ims;

            return View();

        }

Here is the code for my View of the create page:

<div class="form-group">

         @Html.LabelFor(model => model.EmployeeId, "EmployeeName", htmlAttributes: new { @class = "control-label col-md-2" })

        <div class="col-md-10">

            @Html.DropDownList("Contacts", ViewBag.Contacts as List<SelectListItem>, new { @class = "form-control", multiple = "multiple" })

            @Html.ValidationMessageFor(model => model.EmployeeId, "", new { @class = "text-danger" })

        </div>

    </div>

 

    <div class="form-group">

        @Html.LabelFor(model => model.ContactGroupId, "ContactGroupName", htmlAttributes: new { @class = "control-label col-md-2" })

        <div class="col-md-10">

            @Html.DropDownList("ContactGroupId", ViewBag.cg as List<SelectListItem>, new { @class = "form-control", multiple = "multiple" })

            @Html.ValidationMessageFor(model => model.ContactGroupId, "", new { @class = "text-danger" })

        </div>

    </div>


You have created two list in controller side.您已经在控制器端创建了两个列表。 You have added all items in first listing您已添加第一个列表中的所有项目

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

            foreach (var im in cg)

            {

                ims.Add(new SelectListItem

                {

                    Value = im.ContactGroupID.ToString(),

                    Text = im.ContactGroupName.ToString(),

                });

 

            };
  ViewBag.cg = ims;

just change items.Add into ims.Add in for loop只需更改 items.Add 到 ims.Add 循环

while the view for both of them are different at runtime it appears in 1 textbox..虽然它们的视图在运行时不同,但它出现在 1 个文本框中。

From your code, you set all into items从您的代码中,您将所有内容设置为items

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

            foreach (var im in cg)

            {

                items.Add(new SelectListItem

                {

                    Value = im.ContactGroupID.ToString(),

                    Text = im.ContactGroupName.ToString(),

                });

 

            };

            ViewBag.cg = ims;

Change items.Add into ims.Add like:更改项目。添加到items.Add ims.Add

ims.Add(new SelectListItem
    
                    {
    
                        Value = im.ContactGroupID.ToString(),
    
                        Text = im.ContactGroupName.ToString(),
    
                });

暂无
暂无

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

相关问题 一个路由图中可以有多个动作吗? - Can I have more than one action in a single routemap? MVC我如何创建(10)多个文本框并为这些文本框分配一个参数。 我也想将参数传递给我的控制器 - MVC how can i create (10)multiple textbox and have one parameter assign to those text box. I also want to pass the parameter to my controller 显示使用Html.List的列表框,我有一个列表 <User> 采集 - displaying a listbox using Html.List, and I have a List<User> collection 如何重用我创建的代码来保存 select 列表项 - How can I reuse code that I have created to hold a select list item 我必须验证.NET MVC中的文本框,其中文本框的值小于或等于两个列值的差? - I have to validate a text box in .NET MVC where the text box value is less than or equal the difference of two column values? 我如何在MVC中有一个无到一个或多个参数列表 - How can I have a none to one or many parameter list in MVC 在MVC 4剃须刀中一个接一个地选中列表中的每个复选框后如何调用Ajax函数 - How can I call Ajax function after check each check box in a list one by one in mvc 4 razor 在kendo UI组合框下进行选择时,如何显示特定文本? - How do I have specific text appear when a selection is made under kendo UI Combo box? 我怎样才能只设置一个文本 - how can I set only one text from <a attribute list? 在下拉列表框中显示项目 - Displaying items in dropdown list box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM