转换 IListMVC.SelectListItem 需要显式转换

发布于 2024-12-04 00:04:32 字数 640 浏览 0 评论 0原文

我正在调用一个使用 IList 的项目(WCF,但这不重要),使用 MVC 3 (这显然也不重要)我想转换单列字符串列表(IList 这只是一个列表)国家)到列表

硬编码列表我已经这样做了,它们工作正常:

 public List<SelectListItem> mothermarriedatdelivery = new List<SelectListItem>();
 mothermarriedatdelivery.Add(new SelectListItem() { Text = "Yes", Value = "1" });

但是,现在我正在尝试转换此代码:

 public List<SelectListItem> BirthPlace { get; set; }
 BirthPlace = new List<SelectListItem>();
 GetHomeRepository repo = new GetHomeRepository();
 BirthPlace = repo.GetCountries();

我需要从列表隐式转换为 SelectListItem,有人这样做吗?是的..我搜索并找到了几个例子,但没有一个真正适合我的特定需求。

I am calling a project (WCF, but that shouldn't matter) that consumes a IList , with MVC 3 (which really should not matter either obviously) I want to convert a single column List of strings (IList which is just a list of Countries) into a List

Hard Coded list I have done like this and they work fine:

 public List<SelectListItem> mothermarriedatdelivery = new List<SelectListItem>();
 mothermarriedatdelivery.Add(new SelectListItem() { Text = "Yes", Value = "1" });

However, now I am trying to convert this code:

 public List<SelectListItem> BirthPlace { get; set; }
 BirthPlace = new List<SelectListItem>();
 GetHomeRepository repo = new GetHomeRepository();
 BirthPlace = repo.GetCountries();

I need to implicitly convert from the List to SelectListItem, anyone do this? Yes.. I have search and found several examples, but none that really fit my specific need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断念 2024-12-11 00:04:32

您可以这样使用 LINQ:

BirthPlace = repo.GetCountries()
                 .Select(x => new SelectListItem { Text = x, Value = x })
                 .ToList();

或者我认为您可以只使用 SelectList 的构造函数之一:

public SelectList BirthPlace { get; set; }

BirthPlace = new SelectList(repo.GetCountries());

You can use LINQ as such:

BirthPlace = repo.GetCountries()
                 .Select(x => new SelectListItem { Text = x, Value = x })
                 .ToList();

Or I think you can just use one of SelectList's constructors:

public SelectList BirthPlace { get; set; }

BirthPlace = new SelectList(repo.GetCountries());
莫相离 2024-12-11 00:04:32

在控制器中,

   var result = proxy.GetAllLocations();

   ViewBag.Area = result.Where(p => p.ParentId == null).Select(p => new SelectListItem { Text = p.LocationName, Value = p.LocationId.ToString() }).ToList();

   ViewBag.Locations = result.Where(p => p.ParentId != null).Select(p => new SelectListItem { Text = p.LocationName, Value = p.LocationId.ToString() }).ToList();

   return View();

在视图中,

@Html.DropDownList("LocationName", ViewData["Area"] as IEnumerable<SelectListItem>) 

In Controller ,

   var result = proxy.GetAllLocations();

   ViewBag.Area = result.Where(p => p.ParentId == null).Select(p => new SelectListItem { Text = p.LocationName, Value = p.LocationId.ToString() }).ToList();

   ViewBag.Locations = result.Where(p => p.ParentId != null).Select(p => new SelectListItem { Text = p.LocationName, Value = p.LocationId.ToString() }).ToList();

   return View();

In View ,

@Html.DropDownList("LocationName", ViewData["Area"] as IEnumerable<SelectListItem>) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文