使用jquery脚本时如何访问方法?

时间:2014-04-11 04:45:51

标签: c# javascript jquery asp.net asp.net-mvc

我有一个 Jquery脚本,每当下拉列表值发生变化时,它应该取值并将其传递给一个方法,该方法获取值并根据值计算价格,然后在最后为新值设置标签。

这是我的 Jquery脚本:

@section PageScripts{
<script type="text/javascript">
    $(document).ready(function () {
        $('#paperTypeJList').change(function () {
            // trying to figure out how to pass the value to the following method
        });
    });
</script>
}

以下是我尝试在 Jquery脚本中调用的方法:

public decimal getNewPrice(string dropdownValue)
    {
        // do something with value and return a decimal
        return 0;
    }

4 个答案:

答案 0 :(得分:1)

您必须使用jquery向服务器发送ajax调用。

这样的事情:

<script type="text/javascript">
    $(document).ready(function () {
        $('#paperTypeJList').change(function () {
            // trying to figure out how to pass the value to the following method
      var value = $(this).val();

 $.ajax({
  url: '@Url.Action("Action","Controller")',
  data: {dropdownValue:value},
  cache: false,
  success: function(response){
    alert(response);
  }
});

    });

    });
</script>

答案 1 :(得分:0)

使用jquery Ajax尝试此操作 Html:

 <label id="priceLabel"></label>

的Javascript

   $(document).ready(function () {
    $('#paperTypeJList').change(function () {
        $.ajax({
            type: "GET",
            url: "/Home/getNewPrice?dropdownValue=" + this.value,
            async: true,
            success: function (result) {
                document.getElementById('priceLabel').innerHTML = result;
            }
        });
    });
});

控制器

public decimal getNewPrice(string dropdownValue)
    {
        // do something with value and return a decimal
        return 5.72M;
    }

答案 2 :(得分:0)

$(document).ready(function () {
        $('#paperTypeJList').change(function () {
            getNewPrice($(this).val());
        });
    });

答案 3 :(得分:0)

我目前的代码如下:

@section PageScripts{
<script type="text/javascript">
    $(document).ready(function () {
        $('#paperTypeJList').change(function () {
            $.ajax({
            type: "GET",
            url: "/Home/getNewPrice?dropdownValue=" + this.value,
            async: true,
            success: function (result) {
                  document.getElementById('priceLabel').innerHTML = result;
            }
        });
        });
        });
</script>

}

public decimal getNewPrice(string dropdownValue)
    {
        // do something with value and return a decimal
        return 5.72M;
    }