gpt4 book ai didi

asp.net-mvc - jQuery Ajax 调用 Controller

转载 作者:行者123 更新时间:2023-12-03 22:55:18 25 4
gpt4 key购买 nike

我是 Ajax 新手,如果在下拉列表中选择了某些项目,我会尝试禁用复选框。我需要将 mlaId 传递给 RecipientsController.cs 中的 GetMlaDeliveryType(int Id) 方法。

我不太确定如何在 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);
}
}
});

}

最佳答案

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

}
});

然后修复您的 Controller 操作,使其返回 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
}

关于asp.net-mvc - jQuery Ajax 调用 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12559515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com