![](/img/trans.png)
[英]How to add ListItem to DropDownList from code and get the selected value
[英]How do I get email from selected ListItem which is an object
我有一個顯示名字和姓氏的列表框。 列表框有16個對象,並顯示16個名稱。 列表中的每個項目都是一個對象。 每個對象都有一個名稱,該名稱顯示在列表框中。 該對象也有一封電子郵件。 我的問題是,如何在列表框中獲取所選名稱的電子郵件並將其放入字符串中? 稍后,我將使用此變量將電子郵件發送到列表框中的任何選定名稱。
我的代碼的問題是ListItem.ToString())擁有顯示的名稱,而不是電子郵件。 例如,在列表框中選擇Joe Doe作為名稱(ListItem)。 Joe Doe的電子郵件在對象內部。 當我將鼠標懸停在具有16個對象的staffEmails上時,我通過復制Expression查找Joe Doe電子郵件。 這是Joe Doe電子郵件的表達方式
(new System.Collections.Generic.Mscorlib_CollectionDebugView
<FTACaseReset.Models.GetRequestorInfoModel>(staffEmails).Items[1]).Email
mailMessage.To.Add(to);
List<GetRequestorInfoModel> staffEmails = new List<GetRequestorInfoModel>();
staffEmails =
await FTACaseReset.Controllers.RequestorInfoController.GetAllRequestorInfoes();
ListBoxItem staffEmail = new ListBoxItem();
staffEmail.Text = staffEmails[0].Email;
foreach (var ListItem in MyListBox.SelectedItems)
{
MailAddress to = new MailAddress(ListItem.ToString());
}
這是有關如何填充列表框的代碼
public async void PopulateAdditionalStaffEmailListBox()
{
List<GetRequestorInfoModel> staffEmails = new List<GetRequestorInfoModel>();
try
{
staffEmails = await FTACaseReset.Controllers.RequestorInfoController.GetAllRequestorInfoes();
staffEmails = staffEmails.OrderBy(x => x.DisplayName).ToList();
for (int i = 0; i < staffEmails.Count; i++)
{
ListBoxItem staffEmail = new ListBoxItem();
staffEmail.Text = staffEmails[i].DisplayName;
staffEmail.Text = staffEmails[i].Email;
AdditionalStaffEmailListBox.Items.Add(staffEmail.Text).ToString();
}
}
}
我在這里猜測您的環境,但是您可以嘗試...
...使用Value屬性填充列表框以存儲電子郵件...
staffEmail.Text = staffEmails[i].DisplayName;
staffEmail.Value = staffEmails[i].Email; // set Value property to be email
..以及發送電子郵件時...
foreach (var ListItem in MyListBox.SelectedItems)
{
MailAddress to = new MailAddress(ListItem.Value.ToString()); // get email from Value property
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.