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.
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);