views:

520

answers:

1

I have followed Phil Haack's tutorial and everything works fine but now I want to implement search filters in my grid and I noticed that it sends a filter string attribute with my search string to my controller but its in json format... I have no idea how to get the values and use them to implement my search function..., Im using linq to sql.... please help

Right now Im just trying to implement a search by name function of Companies. I have a Company linq to sql model. I want the search to use SQL Like... so if I have a Company with the name of "Ford" and I type "For" it should find the company Ford and return it to the grid.

A: 

I don't have a lot of time to answer this sorry so here is code I implemented. Hope this helps you out. Leave a comment if it doesn't and I'll try to explain it.

In the view;

function filterBy(filter) {
    $.post("/Admin/jQueryUserFilter", { filterBy: filter }, function(newUserListHTML) {
    $("#divUsers").fadeOut(300, function() {
        document.getElementById("divUsers").innerHTML = newUserListHTML;
    });

    $("#divUsers").fadeIn(300);
});
}


<input type="submit" value="Find User" onclick="filterBy(document.getElementById('txtFor').value);return false;" />

In my controller;

public ActionResult jQueryUserFilter(string filterBy)
{
    AdminRepository<User> adminRepository = new AdminRepository<User>();
    IQueryable<User> users;

    if (filterBy == "**all**")
        users = adminRepository.All().OrderBy(x => x.userName);
    else
        users = adminRepository.All().Where(u => u.userName.StartsWith(filterBy)).OrderBy(x => x.userName);
    return PartialView("UserList", users);
}
griegs
Actually, just looking at it it probably doesn't do the job the best way. Think I was pressured to get it done on that day. Needs a re-write me thinks. But hey, it works.
griegs
the thing is, jquery grid already provides that functionality... it already sends a POST array to the controller method.. and it has a filters attribute in json format with the data required to do the search... I dont know how to access Json Data from inside a C# method.
NachoF
My understanding was that you pass the jQuery data as parameters to your controller. So if you have a set of strings then each string is represented in your controller method. Pretty sure you can pass over a model object too.
griegs