MVC Razor按钮单击甚至传递参数

Ero*_*ocM 11 .net html c# asp.net-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>
}
Run Code Online (Sandbox Code Playgroud)

哪个调用此方法:

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

反过来调用这个局部视图:

@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 />
Run Code Online (Sandbox Code Playgroud)

最终显示这个:

在此输入图像描述

这一直到现在为止.

我想要的是能够点击客户姓名旁边的按钮,然后拉出客户的账户.

如何将该客户与该按钮联系起来以了解该如何拉起以及如何将该按钮单击以将其拉出?

Dar*_*ren 10

单击按钮后,您需要返回客户编号:

如果您在模型中将客户编号作为属性,则可以执行以下操作:

<input id="btnShowCustomer" data-customerNumber="@usr.CustomerNumber" />
Run Code Online (Sandbox Code Playgroud)

然后,您可以POST使用@ Html.ActionLink,@ Ajax.ActionLink或jQuery将此数据发送到服务器:

行动链接

@Html.ActionLink("LoadInfo", "Info", new {customerId=@usr.CustomerNumber})
Run Code Online (Sandbox Code Playgroud)

jQuery的

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

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

       } 
 }); 
Run Code Online (Sandbox Code Playgroud)