telerik kendoui grid not getting assigned with the

2019-09-04 19:27发布

问题:

Presently I have an issue with loading the grid depends on parameter passed to the grid. I am developing a search page In which I have a textbox and button. I would display the grid on the click event of button taking textbox input text as parameter.

My textbox and button:

 <divid="SearchSection">
    <input  type="text"id="txtSearch"class="k-textbox"/> 
    <buttonid="btnSearch"  class="k-button"style="width:150px">Search</button>
</div>

My grid:

        <divid="ADUserSection"> 
        List of users in Active directory:
               @(Html.Kendo().Grid<ADUser>()
                     .Name("kADUser")
                    .Columns(columns =>
                                  {
                                   columns.Bound(p => p.UserLoginName);
                                   columns.Bound(p => p.UserDisplayName);
                                  })
                     .AutoBind(false)
                     .DataSource(ds => {
                                         ds.Ajax()
                                         .Read(read =>
                                           {
                                            read.Action("GetADUser", "ManageUsers").Data("AdditionalData");
                                            });
                                         })

                            )
               </div>

My JavaScript in which I am passing the additional Data:

       function AdditionalData() {
         debugger;
         var text = $("#txtSearch").val().trim();

          return{ searchText: text };   
            *****The Problem happens here: The searchText never get assigned value intext ****
               }

My script in which I am calling the click event and capturing the textbox input:

   <script>
       $(document).ready(function () {
         $("#ADUserSection").fadeIn();
       $("#btnSearch").click(function () {
              debugger;
         var text =AdditionalData().toString();
          var grid = $("#kADUser").data("kendoGrid");
       grid.dataSource.read({searchText:text});
         });
          });
        </script>

My controller Method:

   publicJsonResult GetADUser([DataSourceRequest] DataSourceRequest request, string searchText) {
        viewmodel.searchedADUser = model.GetUserFromAD(searchText);
          return Json(viewmodel.searchedADUser.ToList().ToDataSourceResult (request), JsonRequestBehavior.AllowGet);
        } 

Model:

         public class ADUser
          {
          public string UserLoginName { get; set; }
                public string UserDisplayName { get; set; }
            }
           public List<ADUser> GetUserFromAD(string name) //Get Network Users (AD)
                {
                  var searchUser = newList<ADUser>();
                 var domainContext = newPrincipalContext(ContextType.Domain);
                    var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext,           IdentityType.SamAccountName, "Domain Users");
                  UserPrincipal user = newUserPrincipal(domainContext);
                 if (!String.IsNullOrEmpty(name))
                    {
                      user.Enabled = true;
                       user.Name = name + "*";
                          PrincipalSearcher pS = newPrincipalSearcher();
                           pS.QueryFilter = user;
                            PrincipalSearchResult<Principal> results = pS.FindAll();

                            foreach (var item in results)
                             {
                                  var users = new FMSystemWeb.Models.ADUser();
                                   users.UserLoginName = item.SamAccountName;
                                   users.UserDisplayName = item.DisplayName;
                                   searchUser.Add(users);
                              }
                         }
                       return searchUser;
                       }

My ViewModel:

             public class ViewModelManageUsers
              {
                public List<UserRoleList> assignedUserRole { get; set; }
                public List<ADUser> searchedADUser { get; set; }
                public List<AvailableRoles> availableRoles { get; set; }
              }

Please help me in looking into the issue. I have gone through various posts which explains such scenarios and had tried to implement that, but no luck.

The javascript function

 function AdditionalData() {
      never gets the data assigned to my parameter 
    searchText

hence my controller method always gets null in the parameter and the grid don't display any result.

回答1:

On demand event

  // Bouquet combo change event
    $('#BooketID').live('change', function () {
        FillChannelGridByBouquetID();
    });

set parameter for grid load

/* Load kendo grid by bouquetID*/
    function FillChannelGridByBouquetID() {
        var a = {};
        a.BooketID = $("#BooketID option:selected").val();
        var mAGrid = $('#CustomerPackageChannelKendoGrid').data('kendoGrid');
        mAGrid.dataSource.read(a);
        }

Grid

$("#CustomerPackageChannelKendoGrid").kendoGrid({
        dataSource:
        {
            transport:
            {
                read: "ChannelReadByBooketID",
            },
            schema:
            {
                model:
                {
                    fields:
                    {
                        SelectColumn: {type: "boolean"},
                        ChannelID: { type: "string" },
                        BooketName: { type: "string" },
                        ChannelName: { type: "string" },
                    }
                }
            },
            pageSize: 20,
            serverPaging: false,
            serverFiltering: false,
            serverSorting: false
        },
        height: 200,
        filterable: true,
        groupable: true,
        sortable: true,
        resizable: true,                                                                
        pageable:
        {
            refresh: false,
            pageSizes: [20, 40, 60, 80, 100]
        },
        columns:
        [
            {
                field: "SelectColumn", title: " ", width: "5%",
                template: "<input type='checkbox' #= SelectColumn ? checked='checked':'' # class='chkbx' />"
            },

            { field: "ChannelID", title: "ChannelID", hidden: true, filterable: false, sortable: false },
            { field: "BooketName", title: "Bouquet Name", width: "30%" },
            { field: "ChannelName", title: "Channel Name", width: "30%" },
        ]
    });

Controller

public JsonResult ChannelReadByBooketID(CustomerPackageViewModel booketModel)
        {
            var models = GetAllChannelByBooketID(booketModel.BooketID.ToString());
            return Json(models, JsonRequestBehavior.AllowGet);
        }      

 private List<ChannelViewModel> GetAllChannelByBooketID(string pBooketID)
        {
            Guid gBooketID;

            if (!Guid.TryParse(pBooketID, out gBooketID))
            {
                return null;
            }

            var ViewModels = _bCService.BillingUnit.ChannelRepository.Get(t => t.BooketID == gBooketID).ToList().Select(
                 md => new ChannelViewModel
                 {
                     ChannelID = md.ChannelID,
                     ChannelName = md.ChannelName,
                     BooketID = md.BooketID,
                     BooketName = md.DJBL_tblBooket.BooketName

                 }).OrderBy(o => o.BooketName);

            return ViewModels.ToList();
        }