Skip to content

Instantly share code, notes, and snippets.

@jschmid
Last active September 26, 2015 20:18
Show Gist options
  • Save jschmid/1153585 to your computer and use it in GitHub Desktop.
Save jschmid/1153585 to your computer and use it in GitHub Desktop.
Handling arrays in POST data with Nancy
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Nancy;
namespace NancyComponents
{
public class PostArrayHandler
{
public static object GetFormWithArrays(dynamic form)
{
var newDictionnary = new DynamicDictionary ();
var memberNames = (IEnumerable<string>) form.GetDynamicMemberNames ();
foreach (var key in memberNames)
{
string value = form[key];
var match = PostArrayHandler.reg.Match (key);
if (match.Success)
{
string arrayName = match.Groups[1].Value;
List<string> list = newDictionnary[arrayName].Value;
if (list == null)
{
list = new List<string> ();
newDictionnary[arrayName] = list;
}
list.Add (value);
}
else
{
newDictionnary[key] = value;
}
}
return newDictionnary;
}
private static readonly Regex reg = new Regex (@"^([^\[]+)\[[^\]]?\]$");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment