0

I have a drop down list in C# asp.net MVC3 razor engine. I need to load values to that dropdownlist from one of my tables in my database and some values are hard coded. So I need to get both kind of values into one dropdown list.

I can do them separately.

This is how my view is :

@Html.DropDownListFor(model => model.MyTransaction.Status, new MultiSelectList(ViewBag.MyStatusId, "ID", "Name"))

My Model where enums are created :

public enum Ntypes{
  halfday
  casual
}

My Controller :

ViewBag.MyTransaction = db.LeaveTypes.ToList(); //get the table values to drop down

//then even I can get the hard coded values separately ............

ViewBag.MyTansaction = (from NewLeaveTypes t in Enum.GetValues(typeof(Ntypes))
                                select new { ID = t, Name = t.ToString()).ToList();

But cant get both values into one dropdownlist. Plzzzz Help.

Thanks...........

4

1 回答 1

2

您可以将 2 个列表连接在一起:

var nTypes = Enum
    .GetValues(typeof(Ntypes))
    .Select(t => new LeaveType { ID = t, Name = t.ToString())
    .ToList();
ViewBag.MyTransaction = db.LeaveTypes.ToList().Concat(nTypes);

然后在视图中:

@Html.DropDownListFor(
    model => model.MyTransaction.Status, 
    new SelectList(ViewBag.MyTransaction, "ID", "Name")
)
于 2012-05-24T06:27:08.773 回答