[英]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.