In Controller, get all personels with the _service.GetPersonel() method.To select multiple personels, should define Multiple before SelectList. If you select only one personel, can define new SelectList(). In ViewBag we shsould store personels' name and surname in Text, idtPersonel in Value.
public ICollection<int> idtPersonels{ get; set; }
ViewBag.Personeller = new MultiSelectList( (_service. GetPersonels())
.Select(x => new SelectListItem
{
Text = x.Name+ " " + x.Surname+ " " + x.ID,
Value = x.idtPersonels.ToString()
}), "Value", "Text");
In View, users store selected personels in idtPersonels. To use multiple property of DropDownListFor , create class and call select2 and make @multiple property true. Please visit what is Select2 to get more info.
<div class="form-group">
@Html.DropDownListFor(m => m.idtPersonels, (MultiSelectList)ViewBag.Personeller , new { @class = "form-control select2", @multiple = true })
</div>
public ActionResult GetPersonel(FormCollection form)
{
//in form["idtPersonels"] store id like : {12,45,85}, and we must split them
string[] listOfPersonel = form["idtPersonels"].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
No comments:
Post a Comment