FormCollection没有填充

时间:2013-05-27 17:41:42

标签: c# html asp.net-mvc

我正在开发一个MVC项目,我希望传递一个FormCollection,填充表单数据,并将其发布到我的控制器中的action方法。以下是该视图的示例:

<asp:TextBox ID="txtClientLastName" name="txtClientLastName" runat="server" class="focus"/>

(是的,我知道!MVC视图中的ASP控件并不好,但这就是我所拥有的。标签之间的视图中有后端代码,这就是我没有替换它们的原因)

我用帮助器链接到我的动作:

<%=Html.ActionLink("Save","ClientInformationEdit",new {id=Model.PersonId})%

调用我的行动:

 public ActionResult ClientInformationEdit(int id, FormCollection form)
   {
        //rptLOA_GridCommands(form, id);

       CIHelper ch = new CIHelper();


       ch.person.LastName = form["txtClientLastName"]; 
       db.SaveChanges();

        return View(ch);
    }

我的passes the correct value but FormCollection form is null so form["txtClientLastName"] is null and I don知道原因。

2 个答案:

答案 0 :(得分:0)

添加如下所示的帖子属性:

     [HttpPost]
 public ActionResult ClientInformationEdit(int id, FormCollection form)
   {
        //rptLOA_GridCommands(form, id);

       CIHelper ch = new CIHelper();


       ch.person.LastName = form["txtClientLastName"]; 
       db.SaveChanges();

        return View(ch);
    }

如果您想点击超链接发布表单,请添加以下代码:

<%=Html.ActionLink("Save","ClientInformationEdit",new {id=Model.PersonId,onclick="document.getElementById('form-id').submit();"})%>

答案 1 :(得分:0)

试试这个:

@using (Html.BeginForm("Save", "ClientInformationEdit", FormMethod.Post))
{
    <input type="hidden" value="@Model.PersonId"/>
    <asp:TextBox ID="txtClientLastName" name="txtClientLastName" runat="server" class="focus"/>    
    <input type ="submit"/>
}
在您的视图中

相关问题