How to pass collection from view to controller in asp.net MVC4

asp.net asp.net-mvc asp.net-mvc-4 c# entity-framework-6

Question

I have a drop-down list of talents that I want to send from my view to the controller action.

         <select name="skills">
           <option value="0">Java</option>
           <option value="1">C++</option>
           <option value="2">Fortran</option>
           <option value="3">ASP</option>
         </select>

I want the user to be able to choose many skills from a drop-down menu, save those values in an array as a collection, and then submit that array to an action in the controller in the manner shown below (employee and skills have a many-to-many relationship):

 [HttpPost]
 public ActionResult AddEmp(Employee emp ,IEnumerable<Skills> skills )

 {

   DBCtx db=new DbCtx();
   db.employees.Add(emp);
   var emp_id=db.SaveChanges();

   var employee=db.employees.Find(emp_id);

   foreach(item in skills)
   {
      var skill = db.skills.Find(item);
      employee.skills.Add(skill);
   }
   db.SaveChanges();

   return View();
   } 

Thank you in advance for your assistance.

1
1
8/20/2017 12:05:27 PM

Accepted Answer

ZZZ_tmp
0
8/20/2017 12:53:07 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow