简体   繁体   English

在Action Link MVC剃须刀中传递参数

[英]Passing a parameter in Action Link mvc razor

I'm trying to pass a value into the action link to call a controller method inside an Iframe this is my code: 我正在尝试将值传递到操作链接中,以在iframe中调用控制器方法,这是我的代码:

controller: 控制器:

    public ActionResult CallFastPay(string monateryAmount)
            {

//some code logic here
            }

view: 视图:

@{
    ViewBag.Title = "FastPay";
    var url = ViewBag.url;
}
<h2>FastPay</h2>
<script src="~/Scripts/jquery-3.1.1.min.js"></script>

<script type="text/javascript">

    $(function() {
        $('#myButton').click(function () {
            var url = '@Url.Action("CallFastPay")'

            var amount = document.getElementById("monateryAmount").value

            $('#myFrame').attr(@Url.Action("CallFastPay", amount));
        });
    });
</script>
<br />
<iframe id="myFrame" width="600" height="500"></iframe>
<br />
<span>Amount</span>
<input type="text" class="form-control" id="monateryAmount" name="monateryAmount" />
<br />
<button id="myButton" onclick="">Submit</button>

I am getting the error that amount does not exist in the current context. 我收到这样的错误:当前上下文中不存在该金额。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Change your script like this, 像这样更改您的脚本,

 $('#myButton').click(function () {
      //var url = '@Url.Action("CallFastPay")'
      var amount = $("#monateryAmount").val();
      $.ajax({
       url:"YourControllerName/CallFastPay/"+amount,
       //Or you can write like this, url: url + '/' + amount,
       type:"Get",
       success:function(data)
        {
         //Your success message here, for example
         alert("Amount submitted successfully!");
        }
       error:function(data)
        {
         //Your error message here
        }
      })
    });

Hope it helps! 希望能帮助到你!

Use as below: As your syntex is wrong. 用法如下:由于您的Syntex错误。

$('#myButton').click(function () {
    var amount = $("#monateryAmount").val();
  $.ajax({
        url:'@Url.Action("CallFastPay","ControllerName")',
        data:{'monateryAmount':amount},
       type:Get,
       success:function()
        {
         //Your success message here
        }
       error:function()
        {
         //Your error message here
        }
      });
});

If you want to display the results in the iframe then you need to set the src attribute. 如果要在iframe中显示结果,则需要设置src属性。

In your razor view you can set the base url using your action link, and setup the javascript: 在剃刀视图中,您可以使用操作链接设置基本网址,并设置javascript:

<script type="text/javascript">

    var baseUrl = '@Url.Action("CallFastPay")'; 

    $('#myButton').click(function () {            

        var amount = $('#monateryAmount').val();
        var url = baseUrl + '/' + amount;

        $('#myFrame').attr('src', url);
    });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM