我想在我的控制器中使用静态和动态值填充 Html.DropDownListFor 。我似乎无法弄清楚如何使这项工作。我尝试了几种不同的方法,但大多数都围绕 (1) 在变量中定义静态值,(2) 尝试附加动态值 (3) 通过 viewModel 将所有值传递到我的局部视图。
这是上述方法的示例。
//retrieve categories from DB
List<Category> categoryFromDb = new GetCategory().GetCategoryDropdownList(MySession.Current.AccountId).FindAll(c => c.StageId == currentStage);
//local variable with static values and trying to add the dynamic values from above
var categoryDropDown = new [] {
new {CategoryId = "", CategoryName = "Select Activity"},
new {CategoryId = "0", CategoryName = "Note"},
new {CategoryId = categoryFromDb[0], CategoryName = categoryFromDb[3]} //THIS IS THE LINE I CAN'T FIGURE OUT
};
var viewModel = new ActivityTimelineViewModel
{
ActivityTimeline = new GetActivity().GetActivityTimeline(MySession.Current.AccountId, MySession.Current.CandidateId),
CategoryList = categoryDropDown,
Date = DateTime.Today
};
我敢肯定,鉴于我的新手能力,我缺少一些简单的东西。-蒂姆