Slicing a FormCollection by keys that start with a certain string

前端 未结 2 1621
感情败类
感情败类 2021-01-12 07:42

Is there a nice linqy way of splitting a FormCollection into a Dictionary that contains only those keys that start wi

相关标签:
2条回答
  • 2021-01-12 08:25
    [HttpPost]
    public ActionResult Index(FormCollection collection)
    {
         Dictionary<string,object> form = new Dictionary<string, object>();
         collection.CopyTo(form);
         return View();
    }
    
    0 讨论(0)
  • 2021-01-12 08:36

    If you're looking for a "nice" way of taking an existing dictionary, producing a new dictionary with copies of keys+values, for a subset of the keys, some LINQ code will do this nicely:

    var appSettings = formCollection.AllKeys
        .Where(k => k.StartsWith("AppSettings."))
        .ToDictionary(k => k, k => formCollection[k]);
    
    0 讨论(0)
提交回复
热议问题