转换 IListMVC.SelectListItem 需要显式转换
我正在调用一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样使用 LINQ:
或者我认为您可以只使用 SelectList 的构造函数之一:
You can use LINQ as such:
Or I think you can just use one of SelectList's constructors:
在控制器中,
在视图中,
In Controller ,
In View ,