Binding Bootstrap Dual ListBox with ASP.NET MVC

I have use duallist box from http://www.virtuosoft.eu/code/bootstrap-duallistbox/  to populate list of staff entity from controller that come back from a json request.

To load unselected and selected staff at the same time and display the unselected staff on the left side of dual listbox while selected one on right side.

In html:

 <select id="dualList" class="dualList" multiple="multiple" name="staffIds" size="10"></select>

In content:

 <%: Styles.Render( <%: Styles.Render( "~/Content/plugins/duallistbox/bootstrap-duallistbox.css" ) %>

In Script:

function initDualList(TRId) {
 $.ajax({
  url: "/RepresentativeAssignment/ListStaff/",
  type: "POST",
  data: { Id: Id},
  dataType: "json"
 }).then(querySuccess);

function querySuccess(data) {
   $(".dualList").html();
   $.each(data, function (index, value) {
    if (TRId.length == 0) {
    var option = $("").val(value.Id).text(value.StaffCode + " - " + value.StaffName);
    } else {
    if (value.StaffCode.indexOf("UnSelected-") >= 0) {
     var staffCodeStr = value.StaffCode.substring(11, value.StaffCode.length);
     var option = $("").val(value.Id).text(staffCodeStr + " - " + value.StaffName);
    }else{
     var option = $("").val(value.Id).text(value.StaffCode + " - " + value.StaffName).attr("selected", "selected");
    }
   } 
   $(".dualList").append(option);
  });

  $('.dualList').bootstrapDualListbox({
   preserveselectiononmove: 'moved',
   moveonselect: false
  });

  //remove 'moveall' and 'removeall' button in the dual listbox
  $('#dualList').parent().find('.moveall').css('display', 'none');
  $('#dualList').parent().find('.removeall').css('display', 'none');
 }
}

In Controller:

[HttpPost]
 public string ListStaff(long? staffId)
 {
   List listofStaff = new List();
   if (TRId == null)
   {
     listofStaff = SystemSeg.getAllActiveStaffNotUseInAssignmentMap();
    }
    else
    {
       var RepresentativeList = SystemSeg.getById((long)staffId);
        var AssignmentBaseOnCodeList =SystemSeg.getAllRepresentativeAssignmentMapByCode(RepresentativeList.Code);

        var AllActiveStaffNotUseInAssignmentMapList = SystemSeg.getAllActiveStaffNotUseInAssignmentMap();

         //Rest of Active Staff to be display on the left side of the duallist
         for (int i = 0; i < AllActiveStaffNotUseInAssignmentMapList.Count; i++)
         {
             User staff = SystemSeg.GetUserById(AllActiveStaffNotUseInAssignmentMapList[i].Id);
              staff.UserCode = "UnSelected-" + staff.StaffCode;
              listofStaff.Add(staff);
          }
    }
    return WebUtil.Serialize(listofStaff);
 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s