How to append values to ViewBag in controller .net core MVC?

I have a load ViewBag in my controller which gets data from my SQL database like below.

ViewBag.students = context.Gen_list.Where(d => d.AppRole == "Leader")                 .Select(d => d.FullName).Distinct(); 

After executing above command, ViewBag.students have 2 values like below

[0] "Mike Igar" [1] "Ram Barb" 

Now within the controller I want to append 2 more values so that ViewBag.students will become like below,

[0] "Mike Igar" [1] "Ram Barb" [2] "Krish Sag" [3] "Paul Dev" 

I googled for more than 2 hours but couldn’t figure out how to get above result. Your help will be much appreciated. Thank you.

Asked on July 16, 2020 in .NET,   ASP.net.
Add Comment
1 Answer(s)

Found a solution. Instead of trying to add list to the ViewBag.Below I am adding the list to variable and than feeding it to ViewBag.

   var consultants = context.Gen_list.Where(d => d.AppRole == "Leader")                  .Select(d => d.FullName).Distinct();ToList();     List<string> weekend = new List<string> {"Krish Sag", "Paul Dev"};         ViewBag.students = students.Concat(weekend); 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.