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:
- public ActionResult Create()
- {
- if (Session["AdminId"] != null)
- {
- ViewBag.Area = _areaContext.AreaMaster.ToList();
- ViewBag.Network = _networkContext.NetworkMaster.ToList();
- return View();
- }
- else
- {
- return RedirectToAction("Login", "Admin", new { area = "Admin" });
- }
- }
- [HttpPost]
- public ActionResult Create(EmployeeMaster packageMaster)
- {
- if (Session["AdminId"] != null)
- {
- if (ModelState.IsValid)
- {
- _employeeContext.EmployeeMaster.Add(packageMaster);
- _employeeContext.SaveChanges();
- packageMaster.EMPEnc = enc.Encrypt(packageMaster.EMPId);
- _employeeContext.SaveChanges();
- return RedirectToAction("Index");
- }
- else
- {
- ViewBag.Area = _areaContext.AreaMaster.ToList();
- ViewBag.Network = _networkContext.NetworkMaster.ToList();
- return View();
- }
- }
- else
- {
- return RedirectToAction("Login", "Admin", new { area = "Admin" });
- }
- }
View:
- <div class="col-md-4 form-group">
- <div class="entry-1">
- @Html.LabelFor(model => model.EMPArea, "Area", htmlAttributes: new { @class = "" })<span style="color:red;">*</span>
- @Html.DropDownListFor(model => model.EMPArea, new SelectList(ViewBag.Area, "ARMName", "ARMName"), new { @class = "form-control ", @multiple = "multiple", @id = "multiArea" })
- </div>
- </div>
-
-
- <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
-
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
-
- <script src="~/Scripts/bootstrap.min.js"></script>
-
- <script src="~/Content/js/bootstrap-multiselect.js"></script>
- <link href="~/Content/bootstrap-multiselect.css" rel="stylesheet" />
- <script type="text/javascript">
-
- $(function () {
-
- $('#multiArea').multiselect({
- includeSelectAllOption: true
- });
- $("#selected")
- .click(function () {
-
- var str = "";
- $("#multiArea option:selected").each(function () {
- str +=$(this).text() + ",";
-
- console.log(str);
- });
- })
- .trigger("change");
-
-
- });
- </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