ASP MVC 3控制器动作未接收到所有参数

时间:2012-08-15 18:38:25

标签: c# asp.net-mvc razor

所以我有一个类似的观点:

...
<input type="text" id="FieldOne" />
<input type="text" id="FieldTwo" />
<input type="text" id="FieldThree" />
...

模仿这个类:

public class Foo{
    public string FieldOne { get; set; }
    public string FieldTwo { get; set; }
    public string FieldThree { get; set; }
}

相应控制器中的动作:

[HttpPost]
public ActionResult View(Foo param)
{
   ...
}

当我提交表单时,Post动作中的参数“param”会正确复制与该类匹配的所有字段的值,但其中一个(例如,FieldOne)除外。这些输入由Html.TextboxFor()生成。

这是一个特殊的问题还是我可能会忘记的?

1 个答案:

答案 0 :(得分:6)

您的输入框无效。它们应如下所示:

// Start Form
<input type="text" id="FieldOne" name="FieldOne" />
<input type="text" id="FieldTwo" name="FieldTwo" />
<input type="text" id="FieldThree" name="FieldThree" />
// End Form

说有没有理由说你没有使用Html助手?根据您的模型,最好按以下方式编写表单:

// Start Form
@Html.TextBoxFor(m => m.FieldOne)
@Html.TextBoxFor(m => m.FieldTwo)
@Html.TextBoxFor(m => m.FieldThree)
// End Form