读书人

MVC dropdownListFor 绑定异常

发布时间: 2013-09-25 11:02:58 作者: rapoo

MVC dropdownListFor 绑定错误
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




[解决办法]
你是不是该仔细找找
[解决办法]
把这个方法移到Utility类中
public static class Utility
{
public static List<SelectListItem> GetEduCenters()
}//

@Html.DropDownListFor(model => model.中心标识,Utility.GetEduCenters() )

不明白为什么新手那么喜欢tostring,虽然我当年也很喜欢tostring

读书人网 >asp.net

热点推荐