繁体   English   中英

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

[英]Passing a parameter in Action Link mvc razor

我正在尝试将值传递到操作链接中,以在iframe中调用控制器方法,这是我的代码:

控制器:

    public ActionResult CallFastPay(string monateryAmount)
            {

//some code logic here
            }

视图:

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

我收到这样的错误:当前上下文中不存在该金额。 任何帮助将不胜感激。

像这样更改您的脚本,

 $('#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
        }
      })
    });

希望能帮助到你!

用法如下:由于您的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
        }
      });
});

如果要在iframe中显示结果,则需要设置src属性。

在剃刀视图中,您可以使用操作链接设置基本网址,并设置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