11

我是 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 />

最终显示如下:

在此处输入图像描述

到目前为止,这一直有效。

我想要的是能够单击客户姓名旁边的按钮并拉出客户的帐户。

我如何将该客户与按钮联系起来以知道要拉起谁以及如何让按钮单击将其拉起?

4

2 回答 2

10

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

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

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

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

动作链接

@Html.ActionLink("LoadInfo", "Info", new {customerId=@usr.CustomerNumber})

jQuery

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

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

       } 
 }); 
于 2012-09-07T13:32:57.217 回答
0

我认为这可以解决问题!oid 将是客户的 id(我认为路径不正确 :))

@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" })

祝你好运 ;)

于 2012-09-07T13:23:19.410 回答