5

私はAjaxが初めてで、ドロップダウンで特定の項目が選択されている場合、チェックボックスを無効にしようとしています。RecipientsController.cs の GetMlaDeliveryType(int Id) メソッドに mlaId を渡す必要があります。

javascript 関数 checkMlaDeliveryType(mlaId) で ajax 呼び出しを設定する方法が正確にはわかりません。

        //  MLA Add  disable express checkbox if delivery type is electronic
        $('.AddSelectedMla').change(function () {

            var deliveryType = checkMlaDeliveryType($('.AddSelectedMla').val());


            // disable express option if delivery type is Electronic
            if (deliveryType == "Mail") {
                $(".mlaExpressIndicator").removeAttr("disabled");
            }else{
                $(".mlaExpressIndicator").attr('checked', false).attr("disabled", true);
            }

        })

        // ajax call to get delivery type - "Mail" or "Electronic"
        function checkMlaDeliveryType(mlaId)
        {
            $.ajax({
                type: "GET",
                url: "/Recipients/GetMlaDeliveryType/" ,
                data: mlaId,
                dataType: ,
                success: 
            });

        }

RecipientsController.cs

    public string GetMlaDeliveryType(int Id) 
    {
        var recipientOrchestrator = new RecipientsOrchestrator();

        // Returns string "Electronic" or "Mail"
        return recipientOrchestrator.GetMlaDeliveryTypeById(Id);
    }

編集:

機能した最終的なJavaScriptの外観は次のとおりです

//  MLA Add  disable express checkbox if delivery type is electronic
$('.AddSelectedMla').change(function () {

    checkMlaDeliveryType($('.AddSelectedMla').val());
})

// ajax call to get delivery type - "Mail" or "Electronic"
function checkMlaDeliveryType(mlaId)
{
    $.ajax({
        type: 'GET',
        url: '@Url.Action("GetMlaDeliveryType", "Recipients")',
        data: { id: mlaId },
        cache: false,
        success: function (result) {
            // disable express option if delivery type is Electronic
            if (result == "Mail") {
                $(".mlaExpressIndicator").removeAttr("disabled");
            } else {
                $(".mlaExpressIndicator").attr('checked', false).attr("disabled", true);
            }
        }
    });

}
4

2 に答える 2

13
$.ajax({
    type: 'GET',
    url: '/Recipients/GetMlaDeliveryType',
    data: { id: mlaId },
    cache: false,
    success: function(result) {

    }
});

次に、コントローラーのアクションを修正して、文字列ではなく ActionResult を返すようにします。あなたの場合、JSONが適切です:

public string GetMlaDeliveryType(int Id) 
{
    var recipientOrchestrator = new RecipientsOrchestrator();

    // Returns string "Electronic" or "Mail"
    return Json(
        recipientOrchestrator.GetMlaDeliveryTypeById(Id), 
        JsonRequestBehavior.AllowGet
    );
}

これで、成功のコールバックがモデルの JavaScript インスタンスに直接渡されます。dataTypeパラメータを指定する必要はありません。

success: function(result) {
    // TODO: use the result here to do whatever you need to do
}
于 2012-09-24T05:53:10.477 に答える
5

dataキーがコントローラーのパラメーター (つまりId)と一致するように、Ajax 呼び出しで設定します。

data: { Id: mlaId },

@Url.Action(actionName, controllerName)また、アクション URL を取得するために使用することをお勧めします。

url: '@Url.Action("GetMlaDeliveryType", "Recipients")'
于 2012-09-24T05:53:09.133 に答える