Based on my co-worker’s code, he is passing HTML attributes into his form declaration in the view using BeginForm and the resulting HTML looks like:
<form action="/Reviewer/Complete" ipbID="16743" method="post">
How can I obtain the ipbID in my Controller code? I’ve trying
HttpContext.Request.QueryString["ipbID"]
… and …
Request.Form["ipbID"]
and I’ve even gone into debug and went through every part of Request.Form I could to see if the value was there somehow. Is it not a good practice to put values such as that in the form tag? Any and all help is appreciated. Thanks.
UPDATE:
I should inform you all that this form is being applied to a cell. The cells are in a dataTable. When I use it returns the first value that was hidden, but none of the subsequent ones.
UPDATE 2:
View
<% 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!");
}
%>
Don’t do as your coworker. It is wrong. There’s no
ipbIDattribute defined on theformtag meaning that you are producing invalid HTML. Also form attributes are never posted to the server so you cannot obtain them.I would recommend you using a hidden field which is far more natural. So instead of:
Try:
And then
Request["ipbID"]or a simple controller action parameter namedipbIDwill give you the required value.