Search code examples
listmodel-view-controllerasp.net-mvc-5dropdownaddition

Add items to an existing list mvc5


(I am a new developer)I have a list that I put in a viewBag to make a dropwdown in my view and I would like to add 3 elements to this list so that we can see them at the end of my dropdown. I make a timesheet for the employees and I have a dropdown of the projects that the person worked during the week and I would like to add at the end of the dropdown the 3 options "Vacancy", "Unplanned Absence", "Planned Absence" if the person has been on leave instead of working.

This is my request for the projects :

var projectAssignment = (from pa in db.ProjectAssignment
                                     join p in db.Projects on pa.ProjectId equals p.ID
                                     where pa.EmployeeId == EmployeeId && pa.StartDate !=null && (pa.EndDate == null || pa.EndDate >= DateTime.Now)
                                     select new ProjectTimesheetList
                                     {
                                         ProjectName = p.ProjectName,
                                         ProjectId = pa.ProjectId
                                     });
ViewBag.ProjectTimeSHeet = projectAssignment;

This is a the modal to add my timehseet enter image description hereand i want to put the 3 daysoff type at the end of dropdown, so in this case after "NatureBooker"

And this is my code of my dropdown:

<select name="' + row + '_' + col + '" class="custom-select" id="tsCell_' + row + '_' + col + '" data-row="' + row + '" data-col="' + col + '">' +
                    '<option value="">----Select----</option>@Html.Raw(projsStr)</select>';

SOLUTION:

var projectAssignment = (from pa in db.ProjectAssignment
                                     join p in db.Projects on pa.ProjectId equals p.ID
                                     where pa.EmployeeId == EmployeeId && pa.StartDate !=null && (pa.EndDate == null || pa.EndDate >= DateTime.Now)
                                     select new ProjectTimesheetList
                                     {
                                         ProjectName = p.ProjectName,
                                         ProjectId = pa.ProjectId
                                     });

            List<ProjectTimesheetList> projectAssignments = projectAssignment.ToList();

            projectAssignments.Add(new ProjectTimesheetList
            {
                ProjectName = "Vacancy",
                ProjectId = -1,
            });

            projectAssignments.Add(new ProjectTimesheetList
            {
                ProjectName = "Unplanned Absence",
                ProjectId = -2,
            });

            projectAssignments.Add(new ProjectTimesheetList
            {
                ProjectName = "Planned Absence",
                ProjectId = -3,
            });

            ViewBag.ProjectTimeSHeet = projectAssignments;

And the result: enter image description here


Solution

  • Still not clear what exactly you want, but if my guess is right you probably want to add "fake" projects to the enumerable you're binding to (not a list, by the way).

    As long as you understand that this is absolutely wrong and grounds for being fired on the spot, here you go:

    ViewBag.ProjectTimeSHeet = projectAssignment
        .Concat(new[]
            {
                new ProjectTimesheetList
                {
                    ProjectName = "Vacancy",
                    ProjectId = -1,
                },
                new ProjectTimesheetList
                {
                    ProjectName = "Unplanned Absence",
                    ProjectId = -2,
                },
                new ProjectTimesheetList
                {
                    ProjectName = "Planned Absence",
                    ProjectId = -3,
                },
            });