2012-12-04 114 views
1

我有以下看法:获得按钮有问题点击值

@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 /> 

我想获得该按钮的点击ACCOUNTID。它列出了登录时的所有帐户。

我得到空不管我怎么引用它:

[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)); 
} 

谁能告诉我什么,我做错了什么?

UPDATE

我提出在视图以下变化:

@Html.Hidden(@usr.AccountId) 

和为:

@Html.Hidden(usr.AccountId) 

我添加了线只是为了验证控制器的代码:

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

两者仍然是空的。

+0

你试过var id = formCollection [“AccountId”]; –

+0

是的,实际上我做了,然后将其更改为.get。相同的结果'null'。 – ErocM

+0

我想这可能是因为你使用的是HiddenFor,它需要一个强类型的视图。 可以将您的视图强类型化或使用Html.Hidden()辅助方法。 –

回答

2

您使用的是被称为CustomerData类的属性。它会产生CustomerData.AccountId你的隐藏字段name attribute.try手工绘制隐藏的标签是这样的:

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

完美!非常感谢你! – ErocM

+0

________好运。 –

0

你要绑定错误的隐藏字段名称

尝试:

@Html.Hidden("AccountId") 

也是在服务器方法后,你可以尝试

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