2012-09-07 18 views
10

我是MVC Razor的新手。MVC Razor按钮点击偶数通过参数

我有这样的观点:

@model SuburbanCustPortal.Models.CustomerModel 

@{ 
    ViewBag.Title = "Customer Summary"; 
} 

<h2>Customer Summary Screen</h2> 
<p> 
    Please select an account below or add an existing account. 
</p> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.") 

    <div> 
    <fieldset> 
     <legend>Existing Accounts</legend> 

     @Html.Action("ExistingAccounts2") 

     <p> 
     <input type="submit" value="Add an Account" /> 
     </p> 


    </fieldset> 
    </div> 
} 

哪些调用此方法:

[Authorize] 
public ActionResult ExistingAccounts2() 
{ 
    return PartialView("ExistingAccounts", _client.RequestCustomersForAccount(User.Identity.Name)); 
} 

后者又调用此局部视图:

@model IEnumerable<SuburbanCustPortal.SuburbanService.CustomerData > 

<br /> 
<br /> 
<table> 

    @if (Model != null) 
    { 
    foreach (var usr in Model) 
    { 
     <tr>  
     <td> 
      <input id="btnShowCustomer" name="btnShowCustomer2" type="submit" value="View"/>   
     </td> 
     <td> 
      @usr.AccountId 
     </td> 
     <td> 
      @usr.Name 
     </td> 
@*  <td> 
      @usr.DeliveryStreet 
     </td>*@ 
     </tr> 
    } 
    } 

</table> 
<br /> 

这最终显示此:

enter image description here

此操作到此为止。

我想要的是能够点击客户名称旁边的按钮,并拉起客户的帐户。

如何将该客户绑定到该按钮以知道该拉出谁以及该按钮如何将其拉出?

回答

7

您需要通过客户号码找回曾经的按钮被点击:

如果有客户数作为模型的属性,你可以这样做:

<input id="btnShowCustomer" data-customerNumber="@usr.CustomerNumber" /> 

然后,您可以POST这个数据到服务器使用@Ht ml.ActionLink,@ Ajax.ActionLink,或jQuery的:

操作链接

@Html.ActionLink("LoadInfo", "Info", new {[email protected]}) 

的jQuery

$("#btnShowCustomer").click(function() { 

var customerId = $("#btnShowCustomer").attr("data-customerNumber"); 
    $.ajax({ 
     type: "POST", 
     data: "customerId=" + customerId, 
     url: '@Url.Action("MyAction", "MyController")', 
     success: function (result) { 

     } 
}); 
+0

出错 - “data-customerNumber对于元素输入无效的属性” –

+0

那么我将如何在我的控制器中获取这些信息? – Danieboy

-1

我认为这可以做到这一点! OID将是客户(我不认为路径是确定的:))

@Ajax.RawActionLink("Action", "Controller", new { oid = '@Model.customerID'}, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "the view you want to show" }, new { id = "btnNewB", @class = "your btn class" }) 

好运的id;)

+0

无法解析符号 “RawActionLink”。我怎样才能看到RawActionLink? – ErocM

+0

嗯..这不会发生什么......你得到了什么确切的错误? –

+0

正是在Visual Studio中:无法解析符号“RawActionLink”。 – ErocM