使用FormCollection获取和使用任何一个特定键的每个值

Las*_*eak 7 c# asp.net-mvc asp.net-mvc-3

我在视图中有一堆列表框,每个列表框都有一个唯一的名称/ ID.提交时,action方法接收相应的数据,即key = listbox name和value(s)=该列表框的所有选定值.

如何获取任何一个键的所有值,并使用foreach语句等对每个值执行所需的操作?看看formcollection中的可用方法,他们得到(索引)而不是get(key)...

谢谢...

Kev*_*oet 13

这应该也可以解决问题

public ActionResult YourAction(FormCollection oCollection)
      {

                    foreach (var key in oCollection.AllKeys)
                    {
                        //var value = oCollection[key];
                    }

                    return View();               
      }
    }   
Run Code Online (Sandbox Code Playgroud)

要么

  public ActionResult YourAction(FormCollection oCollection)
  {

                foreach (var key in oCollection.Keys)
                {
                    //var value = oCollection[key.ToString()];
                }

                return View();               
  }
Run Code Online (Sandbox Code Playgroud)

你不确定这个:

  public ActionResult YourAction(FormCollection oCollection)
  {
                foreach (KeyValuePair<int, String> item in oCollection)
                {
                    //item.key
                    //item.value
                }

                return View();               
  }
Run Code Online (Sandbox Code Playgroud)