MVC dropdownListFor 绑定错误

小D2013 2013-09-13 03:00:45
model:
public class 培训中心
{
[Key]
public Guid 中心标识 { get; set; }
public string 中心名称 { get; set; }
public virtual List<科室> 科室s { get; set; }
}

public class 科室
{
[Key]
public Guid 科室标识 { get; set; }
public Guid 中心标识 { get; set; }
public string 科室名称 { get; set; }
}

生成中心list
public List<SelectListItem> GetEduCenters()
{
ChildEduDbContext db = new ChildEduDbContext();

var quert = db.Centers;
List<SelectListItem> item = new List<SelectListItem>();
foreach (var it in quert)
{
item.Add(new SelectListItem { Text = it.中心名称, Value = it.中心标识.ToString() });

}
return item;
}

绑定
@Html.DropDownListFor(model => model.中心标识.ToString(), ViewData["中心"] as SelectListItem)

错误提示
Compiler Error Message: CS1928: 'System.Web.Mvc.HtmlHelper<ChildEdu.Models.科室>' does not contain a definition for 'DropDownListFor' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor<TModel,TProperty>(System.Web.Mvc.HtmlHelper<TModel>, System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>)' has some invalid arguments

...全文
80 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
moonwrite 2013-09-13
  • 打赏
  • 举报
回复
把这个方法移到Utility类中 public static class Utility { public static List<SelectListItem> GetEduCenters() }// @Html.DropDownListFor(model => model.中心标识,Utility.GetEduCenters() ) 不明白为什么新手那么喜欢tostring,虽然我当年也很喜欢tostring
小D2013 2013-09-13
  • 打赏
  • 举报
回复
实在找不出了
Dotar 2013-09-13
  • 打赏
  • 举报
回复
你是不是该仔细找找

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧