This is a migrated thread and some comments may be shown as answers.
Retain filter settings across requests
0 Answers 20 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jann
Top achievements
Rank 1
Jann asked on 01 Feb 2011, 11:16 AM
Did you ever wonder how you could achieve ins erver binding mode that filter and sorting settings for the grid are retained across edit operations? Here is my solution for it:

public static class MvcHelpers
{
    public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
    {
        foreach (T item in items)
        {
            action(item);
        }
    }   
  
      private static NameValueCollection ExtractGridParameters(NameValueCollection aParams)
    {
        Contract.Requires(aParams != null);
        var lRelevantKeys = aParams.AllKeys
                                .Where(i => i.EndsWith("-filter") || 
                                      i.EndsWith("-page") ||
                                      i.EndsWith("-orderBy"));
  
       var result = new NameValueCollection();
       lRelevantKeys.ForEach(i => result.Add(i, aParams[i]));
       return result;
        }
  
    public static RedirectToRouteResult WithGridSettings(this RedirectToRouteResult result, NameValueCollection aParams)
    {
        ExtractGridParameters(aParams).AllKeys.ForEach(e => result.RouteValues.Add(e, aParams[e]));
  
        return result;
    }
}

Then instead of
return RedirectToAction("Index");

you do
RedirectToAction("Index").WithGridSettings(Request.QueryString);

I hope this is useful to someone.
Please feel free to make suggestions on how to make this better.
Tags
Grid
Asked by
Jann
Top achievements
Rank 1
Share this question
or