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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12318905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:52:50  来源:igfitidea点击:

MVC Razor button click even pass parameter with it

c#html.netasp.net-mvcrazor

提问by ErocM

I'm new to MVC Razor.

我是 MVC Razor 的新手。

I have this view:

我有这样的看法:

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

Which calls this method:

其中调用此方法:

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

Which in turn calls this partial view:

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

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

Which ends up displaying this:

最终显示如下:

enter image description here

enter image description here

This works up to this point.

到目前为止,这有效。

What I want to so is be able to click on the button next to the customer's name and it pull up the customer's account.

我想要的是能够点击客户姓名旁边的按钮,它会调出客户的帐户。

How do I tie that customer to the button to know who to pull up and how do have the button click pull it up?

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

采纳答案by Darren

You need to pass the Customer Number back once the button is clicked:

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

If you have the customer number as a property in the Model you could do something like:

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

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

You could then POSTthis data to the Server using an @Html.ActionLink, @Ajax.ActionLink, or jQuery:

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

Action Link

操作链接

@Html.ActionLink("LoadInfo", "Info", new {[email protected]})

jQuery

jQuery

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

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

       } 
 }); 

回答by Mihai Labo

I think this would do the trick ! oid would be the id of the customer (i dont think the path is ok :) )

我认为这可以解决问题!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" })

good luck ;)

祝你好运 ;)