views:

59

answers:

1

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!");
}
%>
+6  A: 

Don't do as your coworker. It is wrong. There's no ipbID attribute defined on the form tag 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:

<form action="/Reviewer/Complete" ipbID="16743" method="post">

Try:

<form action="/Reviewer/Complete" method="post">
    <input type="hidden" name="ipbID" value="16743" />

And then Request["ipbID"] or a simple controller action parameter named ipbID will give you the required value.

Darin Dimitrov
See my update. I've tried the Hidden feature, but because I'm wrapping the form in a cell of a table and I need the ipbID of that particular row, it's not working. It's returning the first ipbID, but if I select the fifth row, I wasn the fifth ipbID.
XstreamINsanity
`Don't do as your coworker. It is wrong`, AMEN!
Bob Fincheimer
Then you need to write your logic so that it puts the right id in the hidden input (i.e. the one relating to the row that the form is in).
David Dorward
@XstreamINsanity, post a sample code. Right now we can only be guessing what you mean by *selecting a row* (there's no such thing in standard HTML so I guess you've written some code).
Darin Dimitrov
In the IPBGrid.ascx that contains the view data, we use a foreach and iterate through the ipbs and apply the ipb.ID to each hidden statement. If I step through, it does as it should. I was thinking maybe when the page tries to render the Hidden attribute, it's blindly applying it to all cells in that column, the first one that is. Could this be an issue with using the dataTable format or would this be an HTML issue?
XstreamINsanity
@XstreamINsanity, I have absolutely no idea what you are talking about. Sample code please.
Darin Dimitrov
Yeah, sorry it took me so long. I can't copy and paste from my dev machine to my network machine, and thumb drives aren't allowed, so I typed most of it up. Check update 2.
XstreamINsanity
I tried putting a hard coded Hidden value instead of using the Html.Hidden and that worked. Not sure as to why, but it did. I marked yours as the answer. Thanks.
XstreamINsanity