2012-09-07 32 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