如何在ASP.NET MVC中获取未知数量的POST参数?
问题描述:
我有以下控制器:如何在ASP.NET MVC中获取未知数量的POST参数?
class FooController : Controller
{
public ActionResult SomeAction(id)
{
Type t = Type.GetType(id);
object o = Activator.CreateInstance(t);
((MyModel)o).ParseParamaters(PostParameters); // I need to pass the post parameters here
//...
}
}
我想取回已提交了所有的POST参数。
怎么办?
答
你做到这一点与
[HttpPost]
public ActionResult SomeAction(id, FormCollection form)
{
//do what you want with the collection
}
答
我相信是的Request.QueryString是字符串的集合,所以你可以把它作为一个参数ParseParameters。或者你可以传递整个Request对象。
但是我想知道为什么你想要,当有完美的模型绑定内置到MVC做你所有的重担。 http://weblogs.asp.net/nmarun/archive/2010/02/25/asp-net-mvc-model-binding.aspx
+1
因为我无法分辨正在创建哪种类型。 – 2010-08-17 13:23:24
无法找到HttpPost属性。这是为MVC 2. 我使用1.1 – 2010-08-17 13:29:47
我改变了属性AcceptVerbs(HttpVerbs.Post)],它的工作。谢谢。 – 2010-08-17 13:39:52