This is a migrated thread and some comments may be shown as answers.

[Solved] Grid Sort Column

5 Answers 81 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ed Buchsbaum
Top achievements
Rank 1
Ed Buchsbaum asked on 19 May 2010, 10:31 AM
I noticed something strange in terms of the way the grid sorting works. I have a textbox on a view. It is a filter. The user types in a value and clicks submit.  The controller has 2 methods, one for the get and one for the post. Code below. All works fine, the grid displays the filtered results. Pretty standard. However, if I click on a column to sort it, the entire grid disappears. I traced the code and noticed that when I click on a column to sort it, the GET action method is being called rather than the POST action method. It seems to me that clicking on a column does a postback and thus the POST action method should be called not the GET action method. Since the GET action method is being called, it returns a view with no data and hence the grid disappears. Is this by design or am I missing something?

Many Thanks,
Ed

        public ActionResult Filter()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Filter(FormCollection collection)
        {
            using (GroupBuyDataContext db = new GroupBuyDataContext())
            {
                IEnumerable<Deal> results = null;
                if (!string.IsNullOrEmpty(collection["all"]))
                    results = db.Manager.Deal.GetAll();
                else if (!string.IsNullOrEmpty(collection["filter"]))
                    results = db.Manager.Deal.GetByTitle(collection["name"]);
                
                ViewData["contains"] = collection["name"];
                if (results != null)
                    return View(results.ToList());
                else
                    return View();

            }
        }

5 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 19 May 2010, 10:51 AM
Hi Ed Buchsbaum,

By design the grid renders hyperlinks when sorting is enabled. Those use GET requests.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Ed Buchsbaum
Top achievements
Rank 1
answered on 19 May 2010, 07:23 PM
Interesting...

So if I have a page with a filter area like a textbox or dropdown and a submit button and the user enters some criteria and submits the form and it returns a resultset which is bound to the grid, there is no way to sort or page the grid? The only way I can get paging and sorting to work is by getting the data in the get action method. However, in the real world this is a rare situation. It is much more common to have some filter controls on the top with a post to get a subset of the data. Then the user can page through the returned data and sort it.

Is there a way to do this using your grid?

Many Thanks,
Ed
0
Atanas Korchev
Telerik team
answered on 20 May 2010, 07:21 AM
Hello Ed Buchsbaum,

The recommended approach is to add the sort string to the grid binding configuration so you can get it as an action method parameter:


<%= Html.Telerik().Grid<T>()
                  .Name("searchResults")
                  .BindTo((IEnumerable<T>)ViewData["searchResults"])
                  .DataBinding(binding => binding.Server()
                           .Select("Action", "Controller", new { query = ViewData["query"] })));
 %>


Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Ed Buchsbaum
Top achievements
Rank 1
answered on 20 May 2010, 08:19 AM
Thank you. Would you be so kind as to put together a small example site showing fully how to do this? It would be a great help.

Many Thanks,
Ed
0
Atanas Korchev
Telerik team
answered on 20 May 2010, 08:35 AM
Hi Ed Buchsbaum,

Here it is:

View:
<% Html.BeginForm("Search", "Home"); %>
    Search:<%= Html.TextBox("query") %>
    <input type="submit" value="search" />
<% Html.EndForm(); %>
 
<% if (ViewData.ContainsKey("query"))
   { %>
<%= Html.Telerik().Grid((IEnumerable<SearchResult>)ViewData["results"])
        .Name("Grid")
        .Pageable()
        .Sortable()
        .DataBinding(binding => binding.Server()
            .Select("Index", "Home", new { query = ViewData["query"] }))
%>
<% } %>

Controller:
public class SearchResult
{
    public string Text
    {
        get;
        set;
    }
}
 
public class HomeController : Controller
{
    public ActionResult Index(string query)
    {
        if (!string.IsNullOrEmpty(query))
        {
            ViewData["query"] = query;
            ViewData["results"] = Results(query);
        }
         
        return View();
    }
 
    private IEnumerable<SearchResult> Results(string query)
    {
        for (int i = 0; i < 100; i++)
        {
            yield return new SearchResult
            {
                Text = query + " " + i
            };
        }
    }
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Search(string query)
    {
        ViewData["query"] = query;
        ViewData["results"] = Results(query);
        return View("Index");
    }
}

All the best,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Ed Buchsbaum
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Ed Buchsbaum
Top achievements
Rank 1
Share this question
or