2

I'd like to populate a Html.DropDownListFor with both static and dynamic values in my controller. I can't seem to figure out how to make this work. I've tried a few different approaches, but most center around (1) define the static values in a variable, (2) try to append the dynamic values (3) pass all values via the viewModel to my partial view.

Here is a sample of the above approach.

//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
    };

I'm sure given my novice ability I am missing something simple. -Tim

1 Answer 1

2

You'll have to find a common type and than you can put them into a combined collection. Can you make the static values of type Category?

List<Category> categoryFromDb = new GetCategory().GetCategoryDropdownList(MySession.Current.AccountId).FindAll(c => c.StageId == currentStage);
var staticCategories = new[]
{
    new Category{CategoryId = "", CategoryName = "Select Activity"},
    new Category{CategoryId = "0", CategoryName = "Note"}
};

var combinedCategories = staticCategories.Union(categoryFromDb);

Or if the Category won't work than put them into a SelectListItem like this.

List<SelectListItem> selectItemsFromDB =
new GetCategory().GetCategoryDropdownList(MySession.Current.AccountId).FindAll(
    c => c.StageId == currentStage)
    .Select(c=> new SelectListItem{Value = c.CategoryId, Text = c.CategoryName});
var staticCategories = new[]
{
    new SelectListItem{Value = "", Text = "Select Activity"}
    new SelectListItem{Value = "0", Text = "Note"}
};

var combinedCategories = staticCategories.Union(selectItemsFromDB);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your help. I actually started going down your first approach after I posted my questions, but I ran into an issue wit the constructors. I ended up using your second approach with one small edit as my CategoryId is an int..Select(c=> new SelectListItem{Value = c.CategoryId.ToString(), Text = c.CategoryName}).ToList();
Figured out my constructor issue. Forgot to add the default/nullary constructor when I explicitly created my own constructor. That was causing an ORM db call to fail.

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.