0

I'm trying to populate dropdownlist from sql database, but I get the following error:

'System.Char' does not contain a property with the name 'ID'.

Here is my code:

Controller:

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;

View:

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

And my Model:

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

Please help guys, seems simple but i really cant get through this.. And i also want to ask can we filter what record from database populated to that dropdownlist with attribute LeaveType as reference? Thankyou

1
  • 1
    new SelectList(leaver, "ID", "TypeDetail"); (not leaverulequery) Commented Jun 1, 2016 at 8:23

1 Answer 1

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;

In you code for selectedlist, the variable should be leaver. Not the leaverulequery

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.