ErocM
ErocM

Reputation: 4662

Having problems getting the button clicked value

I have the following view:

@using SuburbanCustPortal.SuburbanService

<br />
<br />
<table>

      @if (ViewData["CustomerData"] != null)
       {
         foreach (var usr in (IEnumerable<CustomerData>) ViewData["CustomerData"])
         {

           using (Html.BeginForm("ShowCustomer2", "Customer", FormMethod.Post))
           {
             <tr>              
               @Html.HiddenFor(model => @usr.AccountId)

               <td>
                 <input id="btn" type="submit" value="View"/>               
               </td>

               <td>
                 @[email protected]
               </td>

               <td>
                 @usr.Name
               </td>

               <td>
                 @usr.DeliveryStreet
               </td>

             </tr>            

           }
         }
       }

</table>
<br />

I'd like to get the AccountId of the button clicked. It lists all of the accounts that are on that login.

I'm getting null no matter how I reference it:

[HttpPost]
public ActionResult ShowCustomer2(FormCollection formCollection)
{

  var corpid = MiscClasses.GetCookieInfo.TokenId;
  var acctid = formCollection.Get("AccountId");
  MiscClasses.GetCookieInfo.CurrentAccountGuid = acctid;

  var sb = new StringBuilder();
  sb.AppendLine("SuburbanCustPortal,Controllers.CustomerController.ExistingAccounts");
  sb.AppendLine(string.Format("corpid: {0}", corpid));
  sb.AppendLine(string.Format("acctid: {0}", acctid));
  Logging.LogInfo(sb.ToString(), _asName);

  var cr = new CustomerRequest();
  cr.CompanyId = corpid;
  cr.Account = acctid;

  return View("AccountScreen", _client.GetCustomerByGuid(cr));
}

Anyone tell me what I'm doing wrong?

UPDATE

I made the following change in the view:

@Html.Hidden(@usr.AccountId)

and as:

@Html.Hidden(usr.AccountId)

I added the lines just to verify the controller code:

var acctid = formCollection["AccountId"];
acctid = formCollection.Get("AccountId");

Both are still coming out as null.

Upvotes: 1

Views: 76

Answers (2)

lante
lante

Reputation: 7336

you are binding wrong the hidden field name

try:

@Html.Hidden("AccountId")

also in method post in server you can try

[HttpPost]
public ActionResult ShowCustomer2(int accountId, FormCollection formCollection)

Upvotes: 0

Behnam Esmaili
Behnam Esmaili

Reputation: 5967

you are using property of a class called CustomerData.it will produce CustomerData.AccountId for your hidden field name attribute.try rendering hidden tag manually like this:

<input type="hidden" name="AccountId" value="@usr.AccountId">

Upvotes: 2

Related Questions