MVC Razor按钮点击偶数传递参数 [英] MVC Razor button click even pass parameter with it

查看:105
本文介绍了MVC Razor按钮点击偶数传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC Razor。



我有这个观点:

  @model SuburbanCustPortal.Models.CustomerModel 

@ {
ViewBag.Title =Customer Summary;
}

< h2>客户摘要屏幕< / h2>
< p>
请在下面选择一个帐户或添加一个现有帐户。
< / 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>
$ b $ @(Html.BeginForm())
{
@ Html.ValidationSummary(true,帐户创建失败,请更正错误并重试。)

< div>
< fieldset>
<图例>现有帐户< / legend>

@ Html.Action(ExistingAccounts2)

< p>
< input type =submitvalue =添加帐户/>
< / 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)
{
< tr>
< td>
< input id =btnShowCustomername =btnShowCustomer2type =submitvalue =View/>
< / td>
< td>
@ usr.AccountId
< / td>
< td>
@ usr.Name
< / td>
@ *< td>
@ usr.DeliveryStreet
< / td> * @
< / tr>
}
}

< / table>
< br />

最后显示的是:



点。



我想要的是能够点击客户名称旁边的按钮,并提取客户的账户。



如何将该客户与按钮绑定,以了解谁拉起以及如何让按钮点击?

解决方案

如果您将客户编号作为模型中的一个属性,您需要在点击该按钮后将客户编号返回给客户编号:

您可以执行以下操作:

 < input id =btnShowCustomerdata-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({ b $ b类型:POST,
data:customerId =+ customerId,
url:'@ Url.Action(MyAction,MyController)',
成功:函数(结果){

}
});


I'm new to 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:

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?

解决方案

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 POST this data to the Server using an @Html.ActionLink, @Ajax.ActionLink, or jQuery:

Action Link

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

       } 
 }); 

这篇关于MVC Razor按钮点击偶数传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆