1
        public ActionResult Edit(int? id)
    {
        ViewBag.Role = new SelectList((from t in db.Roles where t.RoleID != 1 && t.RoleID != 2 select t), "RoleID", "RoleName");

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        User user = db.Users.Find(id);

        if (user == null)
        {
            return HttpNotFound();
        }

        return View(user);
    }

The ViewBag.Role is to generate all the Roles Value into the multiselect checkbox.

        <div class="form-group">
        @Html.Label("Roles", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("Role", null, htmlAttributes: new { @multiple = "multiple", @class = "form-control" })
        </div>
    </div>

<script type="text/javascript">
$(function () {
    $('#Role').multiselect({
        includeSelectAllOption: true
    });
});

Basically the MultiSelect CheckBox look like the following image.

enter image description here

Here is my question, how do i precheck the roles that the user already have ?? For example, x user have a role Manager so during edit page Manager checkbox will be checked.

loli pop
  • 75
  • 3
  • 8
  • Your model needs an `IEnumerable` property to bind to, and it needs to be `@Html.ListBoxFor(m => m.yourProperty, (IEnumerableViewBag.Role, ...)` (refer also [Why does the DropDownListFor lose the multiple selection after Submit but the ListBoxFor doesn't?](https://stackoverflow.com/questions/40725358/why-does-the-dropdownlistfor-lose-the-multiple-selection-after-submit-but-the-li/40732481#40732481) –  Jul 28 '18 at 10:22
  • My DropDownList doesnt lose the multiple selection after Submit. I mange to receive the value using array int[] Role. Can u give me an example i dont quite understand what u mean for the first sentence. – loli pop Jul 28 '18 at 10:31
  • Are you serious? Read the link and my comnent - it explains what you need to. And as the link explains, you will never get anything to bind unless its `ListBoxFor()` –  Jul 28 '18 at 10:34

1 Answers1

1

You can do it like this:

$('select').multiselect({includeSelectAllOption: true});

// Get your data
var userRoles="1,3"; // You can use a ViewBag like ViewBag.UserRoles for roles of user

// Split data by ","
var userRolesArray=userRoles.split(",");

// Set dataArray
$("select").val(userRolesArray);

// Refresh for cheking default values
$("select").multiselect("refresh");

You can use a ViewBag like ViewBag.UserRoles for roles of user and fill it in your Action.

Online demo (jsfiddle)

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
  • Thx a lot, it solve my problem. But i dont quite understand what is line {var userRolesArray = userRoles.split(",");} What does the split function do?? – loli pop Jul 28 '18 at 21:55
  • @lolipop For setting value by `.val()`, we should use a array of values as a parameter and `userRoles.split(",")` makes this array from a string like `1,3`, just it. – Ali Soltani Jul 29 '18 at 01:49
  • Thx a lot. Appreciate your help ! – loli pop Jul 30 '18 at 00:08