我正试图完成一个表单,让用户注册到一个asp.net MVC网站,我正在努力获得下拉列表,以正确显示文本。该模型有以下几个方面:
public SelectList CountiesList;在构造函数中,实体是我的数据库上下文:
CountiesList = new SelectList(
from c in entities.Counties
select new SelectListItem
{
Value = c.CountyID.ToString(),
Text = c.County //the county name, nvarchar/string
});在我的cshtml中:
<div class="align-center">
<label>County</label>
@Html.DropDownListFor(x => x.County, Model.DropDowns.CountiesList)
</div>这是用查找表中正确的项目数生成下拉列表,但显示的文本是“System.Web.Mvc.SelectListItem”,而不是县的名称。
任何帮助都是非常感谢的!
发布于 2015-02-01 14:19:53
尝尝这个
public IEnumerable<SelectListItem> CountiesList;
CountiesList = from c in entities.Counties
select new SelectListItem
{
Value = c.CountyID.ToString(),
Text = c.County //the county name, nvarchar/string
};因为你不应该用SelectList填充IEnumerable<SelectListItem>。使用SelectList 或 an IEnumerable<SelectListItem>,,但不能同时使用.
发布于 2015-02-01 14:19:57
我怀疑您的问题比这更深,因为想要将SelectList作为模型是不正常的。我认为在您的模型中存储Dropdowns列表有可能表示概念错误。
您的模型可能应该是您的Entity (一个县的列表?)然后,您可以绑定各州的下拉列表,方法是向Html.DropDownListFor助手传递第二个参数-- SelectList创建的方式与您现在启动CountiesList的方式完全相同。
https://stackoverflow.com/questions/28263448
复制相似问题