Ajax.ActionLink - 传递javascript参数

时间:2016-05-16 15:51:55

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

我用这种方式调用控制器:

@Ajax.ActionLink("Clientes", "ContasClientesPartialView",
                      new {
                          codigoEntidade = -1,
                          numeroFiscal = -1,
                          pais = 1,
                          distrito = 1,
                          concelho = -1
                      },
                      new AjaxOptions {
                          InsertionMode = InsertionMode.Replace,
                          UpdateTargetId = "TomadoresPesquisaID",
                          HttpMethod = "GET",
                          OnBegin = "onBegin",
                          OnComplete = "onComplete"
                      },
                      new
                      {
                          @class = "btn btn-default",
                          @onclick = "$('#EntidadesPesquisaModal').modal('hide'); $('#TomadoresPesquisaModal').modal('show')",
                          @style = "width:100%;", 
                          @id="pesquisaTomadoresButton"
                      })

但我需要传递一个javascript参数,任何人都有解决方案吗?

1 个答案:

答案 0 :(得分:0)

如果你不愿意使用@Ajax.Actionlink;,你可以使用JQuery这样做:

按钮:

<button class="btn blue" id="btnEditAddressConfirm">
    Save
</button>

JQuery的:

$('#btnEditAddressConfirm').on('click', function () {

    var employeeId = $('#hiddenEmployeeId').val();
    var newAddressLine1 = $('#tbNewAddressLine1').val();
    var newAddressLine2 = $('#tbNewAddressLine2').val();
    var newAddressTown = $('#tbNewAddressTown').val();
    var newAddressCounty = $('#tbNewAddressCounty').val();
    var newAddressPostcode = $('#tbNewAddressPostcode').val();

    //Or get your parameter from the rest of your javascript here....

    $.ajax({
        url: '@Url.Action("EditEmployeeAddress", "Employee")',
        type: 'GET',
        dataType: 'json', //only required for returned data
        //we set cache: false because GET requests are often cached by browsers
        //IE is particularly aggressive in that respect
        cache: false,
        data: {
            employeeId: employeeId,
            newAddressLine1: newAddressLine1,
            newAddressLine2: newAddressLine2,
            newAddressTown: newAddressTown,
            newAddressCounty: newAddressCounty,
            newAddressPostcode: newAddressPostcode
        },
        success: function (data) {
            if (data === true) {
                //do whatever you need to do - or do nothing
            }
            else {
                alert("error!!!");
            }
        }
    });
});

并且Controller Action为:

public string EditEmployeeAddress(
    int employeeId,
    string newAddressLine1,
    string newAddressLine2,
    string newAddressTown,
    string newAddressCounty,
    string newAddressPostcode)
{
    //do whatever you need to in here...
}