【问题标题】:FormCollection not populatingFormCollection 未填充
【发布时间】:2013-05-28 01:55:21
【问题描述】:

我正在处理一个 MVC 项目,我想传递一个 FormCollection,填充表单数据,并将其发布到我的控制器中的操作方法。这是视图的示例:

<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);
    }

我的ìdpasses the correct value but FormCollection form is null so form["txtClientLastName"] is null and I dont 知道为什么。

【问题讨论】:

  • 您是否将提交按钮放在表单 html 元素中?
  • 如果可能,发表您的观点。
  • 我也面临同样的问题

标签: c# html asp.net-mvc


【解决方案1】:

添加 post 属性如下:

     [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();"})%>

【讨论】:

  • 感谢您的回复。我ve had problems with adding HttpPost and getting a 404 Error in the past. Theres 已经是视图中的表单标签了
  • 发表您的观点,以便于检查问题
【解决方案2】:

试试这个:

@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"/>
}

在你看来

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2016-07-11
    • 2012-03-28
    • 2020-02-21
    • 2015-02-23
    • 2019-01-31
    • 2013-12-07
    相关资源
    最近更新 更多