使用MVC Razor按钮单击事件传递参数

时间:2012-09-07 13:18:31

标签: c# html .net 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>
}

调用此方法:

[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

这一直到现在为止。

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

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

2 个答案:

答案 0 :(得分:10)

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

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

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

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

行动链接

@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) { 

       } 
 }); 

答案 1 :(得分:-1)

我认为这样就可以了! 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" })
祝你好运;)