Mohamamd Shafi

Mohamamd Shafi

  • NA
  • 3
  • 639

How to pass values of Multiselect as arrays to controller.

Feb 11 2018 6:25 AM
My entity class
-----------------------------------
public class AssignCategories
{
[Key]
public int AssignId { get; set; }
public string CategoryId { get; set; }
}

Create controller
-----------------------------------
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("AssignId,CategoryId")] AssignCategories assignCategories, string[] selectedValues)
{
ViewBag.SubCategories = new MultiSelectList(_context.ParentCategories, "ParentCategoryId", "Name", selectedValues);
if (ModelState.IsValid)
{
_context.Add(assignCategories);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(assignCategories);
} 

Html Tag Helpers
-----------------------------------
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<select asp-for="CategoryId" class="form-control" asp-items="ViewBag.SubCategories" multiple id="MultipleSelection" ></select>
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</form>
I need to pass multiple values to database
-------------------------------------------



When I click on create it only add only one record
---------------------------------------------------



Answers (1)