如何从<form>标签获取值?

Xst*_*ity 3 html c# visual-studio-2008 asp.net-mvc-2

根据我的同事的代码,他使用BeginForm将HTML属性传递到视图中的表单声明中,结果HTML如下所示:

<form action="/Reviewer/Complete" ipbID="16743" method="post">
Run Code Online (Sandbox Code Playgroud)

如何在Controller代码中获取ipbID?我试过了

HttpContext.Request.QueryString["ipbID"]
Run Code Online (Sandbox Code Playgroud)

......而且......

Request.Form["ipbID"]
Run Code Online (Sandbox Code Playgroud)

我甚至进入调试并经历了Request.Form的每一部分,我可以看看价值是否以某种方式存在.在表单标记中放置诸如此类的值不是一个好习惯吗?任何和所有的帮助表示赞赏.谢谢.

更新:我应该通知你所有这个表单正在应用于一个单元格.单元格位于dataTable中.当我使用它时,返回隐藏的第一个值,但不返回任何后续值.

更新2:查看

<% Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<PTA.Models.IPB>>" %>

<%@ Import Namespace="PTA.Helpers"%>

<b>Assigned IPBs</b>

<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
    $('#sharedIPBGrid').dataTable();
  });
</script>

<%
if (Model != null && Model.Count() > 0)
{
%>
<table id="sharedIPBGrid" class="display">
  <thead>
    <tr>
      <th>
        <%=Html.LabelFor(m => m.FirstOrDefault().IPBName) %>
      </th>
      <th>
        <%=Html.LabelFor(m => m.FirstOrDefault().Status) %>
      </th>
      <th>
        <%=Html.LabelFor(m => m.FirstOrDefault().PubDate) %>
      </th>
      <th>
        <%=Html.LabelFor(m => m.FirstOrDefault().ChangeDate) %>
      </th>
      <th>
        <%=Html.LabelFor(m => m.FirstOrDefault().Priority) %>
      </th>
      <th>
        <%=Html.LabelFor(m => m.FirstOrDefault().Errors) %>
      </th>
      <th>
        Start
      </th>
      <th>
        Stop
      </th>
      <th>
        Complete
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
<%
  foreach(IPB ipb in Model)
  {
%>
      //Ignoring everything except for the Complete button as there's a lot of logic in there.
      <td>
<%
         if (ipb.StatusID == (int)PTA.Helpers.Constants.State.InWorkActive)
         {
           using (Html.BeginForm("Complete", "Reviewer", FormMethod.Post, new {ipbID = ipb.ID}))
           {
%>
             <%=Html.Hidden("ipbID", ipb.ID)%>
             <input type="submit" id="btnComplete" value="Complete" />
<%
           }
         }
%>
      </td>
<%
  }
%>
    </tr>
  </tbody>
</table>
<%
}
else
{
  Response.Write("No IPBs found!");
}
%>
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 6

不要做你的同事.这是错的.标签上没有ipbID定义任何属性form意味着您生成了无效的HTML.表单属性也永远不会发布到服务器,因此您无法获取它们.

我建议你使用一个更自然的隐藏场.所以代替:

<form action="/Reviewer/Complete" ipbID="16743" method="post">
Run Code Online (Sandbox Code Playgroud)

尝试:

<form action="/Reviewer/Complete" method="post">
    <input type="hidden" name="ipbID" value="16743" />
Run Code Online (Sandbox Code Playgroud)

然后,Request["ipbID"]或者一个名为的简单控制器动作参数ipbID将为您提供所需的值.

  • `不要做你的同事.这是错误的,AMEN! (3认同)