我在那里有两个ViewBag
,我想制作一个
通过这种方式,我可以获得一个DropDownList
....
var customerAdress = from cl2 in dc.Adresses
where cl2.Customer.User.UserEmail == User.Identity.Name
select cl2;
var Adresses = new SelectList(customerAdress.ToList(), "AdressID", "AdressLine1 ", null);
var Adresses2 = new SelectList(customerAdress.ToList(), "AdressID", "AdressLine2", null);
....
有我的观点:
....
<tr>
<td>Address</td>
<td colspan="3">
@Html.DropDownList("Adresses", (IEnumerable<SelectListItem>)(ViewBag.Adresses))
</td>
</tr>
.....
答案 0 :(得分:2)
您需要将它们连接起来显示在一个下拉列表中,如:
var addressesList = customerAdress
Select(x=> new
{
ID = x.AddressID,
AddressLine = x.AddressLine1 + x.AddressLine2
});
ViewBag.Adresses = new SelectList(addressesList.ToList() , "AdressID", "AddressLine ", null);
并且不要将DropDownList
帮助程序名称设置为与ViewBag
密钥相同,它们应该不同以防止意外行为。
在视图中做:
@Html.DropDownList("SelectAddress",ViewBag.Adresses as SelectList,"Select Address")
当您将DropDownList帮助程序的名称设置为与ViewBag键相同时,它将在ViewBag中查找用于填充DropDownList项的键,在这种情况下,您只需指定name与{{ 1}}键:
ViewBag
希望它有所帮助!
答案 1 :(得分:0)
我的问题并不清楚,它已经包含了一个解决方案的尝试,可能误导了我所拥有的所有解决方案 所以这里我的解决方案我只是在SelectListItem上使用了Group属性
我的控制器
....
var customerAdress = from cl2 in dc.Adresses
where cl2.Customer.User.UserEmail == User.Identity.Name
where cl2.Commands.FirstOrDefault().DeliveryAdressID==cl2.AdressID
select cl2;
var addressesList = customerAdress.
Select(x => new
{
ID = x.AdressID,
AddressLine = x.AdressLine1
});
var addressesList2 = customerAdress.
Select (x => new
{
ID = x.AdressID,
AddressLine = x.AdressLine2
}) ;
var allAdress = new List<SelectListItem>();
var group1 = new SelectListGroup() { Name = "DeliverableAdress" };
foreach (var item in addressesList)
{
allAdress.Add(new SelectListItem() { Text = item.AddressLine, Group = group1 });
}
foreach (var item in addressesList2)
{
allAdress.Add(new SelectListItem() { Text = item.AddressLine, Group = group1 });
}
//var Adresses = new SelectList(addressesList.ToList(), "AdressID", "AddressLine ", null);
ViewBag.allAdress = allAdress;
....
和我的观点
....
<tr>
<td>Address</td>
<td colspan="3">
@Html.DropDownList("allAdress", (IEnumerable<SelectListItem>)ViewBag.allAdress)
</td>
</tr>
....
但实际上我误解了正确的想法是连接2个地址线,因为它只是一个地址的细节而没有我认为的2个地址