locked
Entity Framework and ASP.NET MVC 5 How can I push selected checkoxes into my localDB? RRS feed

  • Question

  • User-1953214 posted

    Hello, 

    In my MVC app i have a list of Employees and list of Project (no hard coded data - everything can be added in via Create View). In Create View for Employee I have field for FirstName, LastName and list of Projects (type checkboxes). So if I create new Project it would be added to the displaying list. I can display this list but I am having troubles with Details View - I want to display FirstName, LastName and list of selected checboxes.

    My models:

    public class Employee
        {
            public Employee()
            {
                this.Projects = new HashSet<Project>();
            }
            [Key]
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public virtual ICollection<Project> Projects { get; set; }
        }
        public class EmployeeViewModel
        {
            public Employee Employee { get; set; }
            public List<SelectListItem> Projects { get; set; }
        }
     public class Project
        {
            public Project()
            {
                this.Employees = new HashSet<Employee>();
            }
    
            [Key]
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public bool IsSelected { get; set; }
            public virtual ICollection<Employee> Employees { get; set; }
        }
        public class EmployeeProject
        {
            [Key]
            public int Id { get; set; }    
            public int EmployeeId { get; set; }     
            public int ProjectId { get; set; }
            public virtual Employee Employee { get; set; }
            public virtual Project Project { get; set; }
        }
    
    

    My controller:

       [HttpGet]
            public ActionResult Create()
            {
                var employeeViewModel = new EmployeeViewModel
                {
                    Employee = new Employee(),
                    Projects = dbProject.Select(),
                };
                return View(employeeViewModel);
            }
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create(Employee employee)
            {
                if (ModelState.IsValid)
                {
                    dbEmployee.Add(employee);
                    return RedirectToAction("Details", new { id = employee.Id });
                }
    
                return View();
            }

    You can see all the code here -> https://github.com/IzaGorska/Project

    The question is - how to save to the new created Employee, List of Projects. I understand that I have many to many relation but I am not sure how to handle it. Please advice.

    Wednesday, February 10, 2021 6:56 PM

All replies

  • User475983607 posted

    The Getting Started tutorial on this site illustrates how to solve this programming problem; steps 5, 6 and 7.

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0

    Wednesday, February 10, 2021 7:38 PM
  • User-1953214 posted

    I did try to find some help there at the begining but i can't still figure out this checkboxes. I managed to display them but how then I can save user's choice. 

    Thursday, February 11, 2021 10:41 AM
  • User475983607 posted

    gorskaiza9

    I did try to find some help there at the begining but i can't still figure out this checkboxes. I managed to display them but how then I can save user's choice. 

    I'm guessing you did not go through the tutorial.  There is a step where Instructors are assigned courses using checkboxes.  Basically the exact problem you are trying to solve. 

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/update-related-data?view=aspnetcore-5.0

    I recommend going through the entire tutorial as it contains a lot of best practices and programming patterns.

    Thursday, February 11, 2021 12:31 PM