English 中文(简体)
绑定硬编码值和数据库值, 将其放入一个下调列表
原标题:Binding hardcoded values and databases values into one dropdownlist

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.

我可以分开做

我的看法是这样的:

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

我的模型 创建 enums 创建 :

public enum Ntypes{
  halfday
  casual
}

我的主计长:

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

这样我就能分开 硬编码的数值............

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.

谢谢...

问题回答

你可以将这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")
)




相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签