Rajesh Gami

Rajesh Gami

  • 79
  • 24.4k
  • 1.3m

Multiple selected dropdown value pass from viewTOcontroller

Jul 23 2018 2:14 AM
language use: asp.net MVC.
How to pass multiple selected value from view to controller and store into database with same ID.
E.x :
 i selected cities in dropdown : Ahmedabad, Baroda, Rajkot then store database in like
 
 ID Name 
 City Status
 1 RG Ahmedabad, Baroda, Rajkot True
 
 how it possible.
      here is my original code
 
Controller:
  1. public ActionResult Create()  
  2.         {  
  3.             if (Session["AdminId"] != null)  
  4.             {  
  5.                 ViewBag.Area = _areaContext.AreaMaster.ToList();  
  6.                 ViewBag.Network = _networkContext.NetworkMaster.ToList();  
  7.                 return View();  
  8.             }  
  9.             else  
  10.             {  
  11.                 return RedirectToAction("Login""Admin"new { area = "Admin" });  
  12.             }  
  13.         }  
  14.         [HttpPost]  
  15.         public ActionResult Create(EmployeeMaster packageMaster)  
  16.         {  
  17.             if (Session["AdminId"] != null)  
  18.             {  
  19.                 if (ModelState.IsValid)  
  20.                 {  
  21.                     _employeeContext.EmployeeMaster.Add(packageMaster);  
  22.                     _employeeContext.SaveChanges();  
  23.                     packageMaster.EMPEnc = enc.Encrypt(packageMaster.EMPId);  
  24.                     _employeeContext.SaveChanges();  
  25.                     return RedirectToAction("Index");  
  26.                 }  
  27.                 else  
  28.                 {  
  29.                     ViewBag.Area = _areaContext.AreaMaster.ToList();  
  30.                     ViewBag.Network = _networkContext.NetworkMaster.ToList();  
  31.                     return View();  
  32.                 }  
  33.             }  
  34.             else  
  35.             {  
  36.                 return RedirectToAction("Login""Admin"new { area = "Admin" });  
  37.             }  
  38.         } 
 View:
  1. <div class="col-md-4 form-group">  
  2.                 <div class="entry-1">  
  3.                     @Html.LabelFor(model => model.EMPArea, "Area", htmlAttributes: new { @class = "" })<span style="color:red;">*</span>  
  4.                     @Html.DropDownListFor(model => model.EMPArea, new SelectList(ViewBag.Area, "ARMName""ARMName"), new { @class = "form-control ", @multiple = "multiple", @id = "multiArea" })  
  5.                 </div>  
  6.             </div>  
  7.   
  8.   
  9. <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  10.   
  11. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  12.   
  13. <script src="~/Scripts/bootstrap.min.js"></script>  
  14.   
  15. <script src="~/Content/js/bootstrap-multiselect.js"></script>  
  16. <link href="~/Content/bootstrap-multiselect.css" rel="stylesheet" />  
  17. <script type="text/javascript">  
  18.   
  19.     $(function () {  
  20.   
  21.         $('#multiArea').multiselect({  
  22.             includeSelectAllOption: true  
  23.         });  
  24.         $("#selected")  
  25.             .click(function () {  
  26.                   
  27.                 var str = "";  
  28.                 $("#multiArea option:selected").each(function () {  
  29.                     str +=$(this).text() + ",";   
  30.                       
  31.                     console.log(str);  
  32.                 });  
  33.             })  
  34.             .trigger("change");  
  35.           
  36.          
  37.     });  
  38. </script> 
 
 
i wll get my value in console.log(str) like Ahmedabad,Rajkot,Surat store only one in database. If any solution then qucikly reply me.
 
Thanks

Answers (1)