I checked with the debugger the content of the MultiSelectList
values have changed. So I saw that after the values of which are chosen selected Value = true;
.
But I did not see in my view. When was run, selected value not visible. Lastly I tried ViewBag
, ViewData
, ListBox()
and few couple things more. I don't see what?
This is my Controller:
public ActionResult Edit(int id)
{
UserDto user = _userService.GetUserById(id);
var companiesSelectList = new MultiSelectList(_companyService.GetCompanies(), "Id", "Name", user.Companies.Select(x => x.Id.ToString()).ToArray());
TempData["CompaniesList"] = companiesSelectList;
return View(user);
}
And my View:
<div class="col-md-10">
@Html.ListBoxFor(model => model.Companies, (MultiSelectList)TempData["CompaniesList"], new { @class = "form-control", @required = "required", @multiple = "multiple" })
</div>
and I used this one for Visualisation
$(document).ready(function () {
$('#Companies').select2({
placeholder: 'Please Select Company',
maximumSelectionSize: 15,
width: 400,
});
});
My friend fix it.
I got selected items in companiesSelectList but I dont set selected values. I need it JQuery like that. First:
public JsonResult UserCompany(string userId)
{
UserDto user = _userService.GetUserById(int.Parse(userId));
return Json(user.Companies.Select(x => x.Id).ToList(), JsonRequestBehavior.AllowGet);
}
Json data carries Companies in which the user is connected I have sent companies Ids.
This Ajax Method Checking selected items:
var userId = $('#Id').val();
$.ajax({
url: "/User/UserCompany",
type: 'POST',
data: { 'userId': userId },
datatype: 'json',
success: function (data) {
$('#Companies').val(data).trigger("change");
}
});
thanks my work friend and whoever is looked at my question. c u another question :)