This project is read-only.

UnbindMany

Apr 5, 2011 at 9:41 PM

I originally forked Unbinder to make my code changes I posted on your blog but after pulling down the current source I saw that you already engineered out the need for a prefix. The only thing I really saw left was the need to unbind multiple items. I was able to write this as an extension method so you could easily add the functionality directly to Unbinder.

 

public static class UnbinderExtensions
{
    public static IDictionary<string, object> UnbindMany(this Unbinder unbinder, params object[] requests)
    {
        var dictionary1 = requests.Select(unbinder.Unbind).SelectMany(x => x).ToDictionary(pair => pair.Key, pair => pair.Value);
        return dictionary1;
    }
}

This extension method might as also be useful to you

 

public static class DictionaryExtensions
{
    public static RouteValueDictionary AsRouteValues(this IDictionary<string, object> dictionary)
    {
        return new RouteValueDictionary(dictionary);
    }
}

 

An example of how I used this

 

[HttpPost]
public ActionResult Query(QueryViewModel queryModel, Pagination pagination)
{
    pagination.Size.IsEmpty(() => pagination.Size = Constants.UI.DefaultPageSize);
    var unboundModel = new Unbinder().UnbindMany(queryModel, new { Size = pagination.Size, }).AsRouteValues();

    if (!ModelState.IsValid)
    {
        _messages.Warning("some warning").With(ModelState);

        return RedirectToAction("Index", unboundModel);
    }
    return RedirectToAction("Query", unboundModel);

}