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.