MVC剃刀按钮点击,甚至通过参数吧(MVC Razor button click even pass

2019-07-31 10:25发布

我是新来的MVC剃刀。

我有这样的观点:

@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 />

这最终显示此:

这工作了这一点。

我想这样是可以点击旁边的客户名称的按钮,并拉起了客户的帐户。

如何客户绑按钮知道谁拉涨以及如何有按钮,点击拉起?

Answer 1:

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

如果你有客户数量在你可以做类似型号的属性:

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

       } 
 }); 


Answer 2:

我认为这会做的伎俩! 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" })

祝好运 ;)



文章来源: MVC Razor button click even pass parameter with it