根据我同事的代码,他使用BeginForm将HTML属性传递到视图中的表单声明中,生成的HTML如下所示:
<form action="/Reviewer/Complete" ipbID="16743" method="post">如何在控制器代码中获取ipbID?我一直在努力
HttpContext.Request.QueryString["ipbID"]..。还有..。
Request.Form["ipbID"]我甚至进入了调试阶段,检查了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!");
}
%>发布于 2010-08-18 21:20:21
不要像你的同事那样做。这是错误的。在form标记上没有定义ipbID属性,这意味着您正在生成无效的HTML.此外,表单属性永远不会发布到服务器,因此您无法获取它们。
我建议你使用一个更自然的隐藏字段。因此,不是:
<form action="/Reviewer/Complete" ipbID="16743" method="post">尝试:
<form action="/Reviewer/Complete" method="post">
<input type="hidden" name="ipbID" value="16743" />然后,Request["ipbID"]或一个名为ipbID的简单控制器操作参数将为您提供所需的值。
https://stackoverflow.com/questions/3512468
复制相似问题