(MVC)带有viewbag错误的Html.DropDownList

时间:2016-06-01 08:21:31

标签: c# asp.net asp.net-mvc

我正在尝试从sql数据库填充dropdownlist,但是我收到以下错误:

  

'System.Char'不包含名称为'ID'的属性。

这是我的代码:

控制器:

string leaverulequery = "select ID,TypeDetail,LeaveType,Max from LeaveRule";
var leaver = db.LeaveRule.SqlQuery(leaverulequery).ToList();
var lists = new SelectList(leaverulequery, "ID", "TypeDetail");
ViewBag.CategoryId = lists;

查看:

@Html.DropDownList("lv_type", (SelectList) ViewBag.CategoryId, "--Select One--", new{  @class = "form-control input-sm"}) 

我的模特:

public class LeaveRule
{
    [Key]
    public int ID { get; set; }
    public string LeaveType { get; set; }
    public int Max { get; set; }
    public string TypeDetail { get; set; }
}

请帮助伙计,看起来很简单,但我真的无法通过这个... 我还想问一下,我们可以使用属性LeaveType作为参考过滤从填充到该下拉列表的数据库中的哪条记录?三江源

1 个答案:

答案 0 :(得分:1)

string leaverulequery = "select ID,TypeDetail,LeaveType,Max from LeaveRule";
        var leaver = db.LeaveRule.SqlQuery(leaverulequery).ToList();
        var lists = new SelectList(leaver, "ID", "TypeDetail");
        ViewBag.CategoryId = lists;

在您选择列表的代码中,变量应该是leaver。不是leaverulequery