如何从 HTTPPOST、 dictionary 或?

我有一个 MVC 控制器,它的操作方法是:

[HttpPost]
public ActionResult SubmitAction()
{
// Get Post Params Here
... return something ...
}

该表单是一个带有简单文本框的非平凡表单。

问题

如何访问参数值?

我不是从一个观点发帖,这个帖子是来自外部。我假设有一个我可以访问的键/值对集合。

我试了 Request.Params.Get("simpleTextBox");,但它返回错误“对不起,在处理您的请求时发生了一个错误。”。

274004 次浏览

您可以让您的控制器操作采取一个对象,该对象将反映表单输入名称,默认的模型绑定器将自动为您创建这个对象:

[HttpPost]
public ActionResult SubmitAction(SomeModel model)
{
var value1 = model.SimpleProp1;
var value2 = model.SimpleProp2;
var value3 = model.ComplexProp1.SimpleProp1;
...


... return something ...
}

Another (obviously uglier) way is:

[HttpPost]
public ActionResult SubmitAction()
{
var value1 = Request["SimpleProp1"];
var value2 = Request["SimpleProp2"];
var value3 = Request["ComplexProp1.SimpleProp1"];
...


... return something ...
}

Simply, you can use FormCollection like:

[HttpPost]
public ActionResult SubmitAction(FormCollection collection)
{
// Get Post Params Here
string var1 = collection["var1"];
}

您还可以使用一个映射有 Form 值的类,asp.net mvc 引擎会自动填充它:

//Defined in another file
class MyForm
{
public string var1 { get; set; }
}


[HttpPost]
public ActionResult SubmitAction(MyForm form)
{
string var1 = form1.Var1;
}

答案是非常好的,但是在最新版本的 MVC 和。NET,我真的很喜欢使用,而不是“老学校”的表单收集和请求键。


Consider a HTML snippet contained within a form tag that either does an AJAX or FORM POST.

<input type="hidden"   name="TrackingID"
<input type="text"     name="FirstName"  id="firstnametext" />
<input type="checkbox" name="IsLegal"  value="Do you accept terms and conditions?" />

Your controller will actually parse the form data and try to deliver it to you as parameters of the defined type. I included checkbox because it is a tricky one. It returns text "on" if checked and null if not checked. The requirement though is that these defined variables MUST exists (unless nullable(remember though that string is nullable)) otherwise the AJAX or POST back will fail.

[HttpPost]
public ActionResult PostBack(int TrackingID, string FirstName, string IsLegal){
MyData.SaveRequest(TrackingID,FirstName, IsLegal == null ? false : true);
}

你也可以在不使用任何剃须刀帮手的情况下发回一个模型。我曾经遇到过一些需要这样做的情况。

public Class HomeModel
{
public int HouseNumber { get; set; }
public string StreetAddress { get; set; }
}

HTML 标记将简单地..。

<input type="text" name="variableName.HouseNumber" id="whateverid" >

您的控制器(Razor Engine)将拦截 Form 变量“ variableName”(名称随您喜欢,但保持一致)并尝试构建它并将其强制转换为 MyModel。

[HttpPost]
public ActionResult PostBack(HomeModel variableName){
postBack.HouseNumber; //The value user entered
postBack.StreetAddress; //the default value of NULL.
}

当控制器期望一个 Model (在本例中是 HomeModel)时,您不必定义所有字段,因为解析器会将它们保留为默认值,通常是 NULL。好处是,您可以混合和匹配不同的模型上的标记和后备解析将填充尽可能多。您不需要在页面上定义模型或使用任何帮助程序。

提示: 控制器中参数的名称是已定义的名称 in the HTML mark-up "name=" not the name of the Model but the name of the expected variable in the !


使用 List<>的标记有点复杂。

<input type="text" name="variableNameHere[0].HouseNumber" id="id"           value="0">
<input type="text" name="variableNameHere[1].HouseNumber" id="whateverid-x" value="1">
<input type="text" name="variableNameHere[2].HouseNumber"                   value="2">
<input type="text" name="variableNameHere[3].HouseNumber" id="whateverid22" value="3">

List < > 上的索引必须始终是从零开始并且是连续的。

[HttpPost]
public ActionResult PostBack(List<HomeModel> variableNameHere){
int counter = MyHomes.Count()
foreach(var home in MyHomes)
{ ... }
}

使用 IEnumerable<>对非零基和非顺序索引后退。我们需要添加一个额外的隐藏输入,以帮助活页夹。

<input type="hidden" name="variableNameHere.Index" value="278">
<input type="text" name="variableNameHere[278].HouseNumber" id="id"      value="3">


<input type="hidden" name="variableNameHere.Index" value="99976">
<input type="text" name="variableNameHere[99976].HouseNumber" id="id3"   value="4">


<input type="hidden" name="variableNameHere.Index" value="777">
<input type="text" name="variableNameHere[777].HouseNumber" id="id23"    value="5">

代码只需要使用 IEnumable 并调用 ToList()

[HttpPost]
public ActionResult PostBack(IEnumerable<MyModel> variableNameHere){
int counter = variableNameHere.ToList().Count()
foreach(var home in variableNameHere)
{ ... }
}

It is recommended to use a single Model or a ViewModel (Model contianing other models to create a complex 'View' Model) per page. Mixing and matching as proposed could be considered bad practice, but as long as it works and is readable its not BAD. It does however, demonstrate the power and flexiblity of the Razor engine.

因此,如果您需要添加一些任意的内容,或者重写来自 Razor 助手的另一个值,或者只是不想为一个使用一些不寻常的数据组合的表单创建自己的助手,您可以快速地使用这些方法来接受额外的数据。

如果您想直接从 Http 请求获得表单数据,不需要任何模型绑定或 FormCollection,您可以使用以下方法:

[HttpPost]
public ActionResult SubmitAction() {


// This will return an string array of all keys in the form.
// NOTE: you specify the keys in form by the name attributes e.g:
// <input name="this is the key" value="some value" type="test" />
var keys = Request.Form.AllKeys;


// This will return the value for the keys.
var value1 = Request.Form.Get(keys[0]);
var value2 = Request.Form.Get(keys[1]);
}