I\'m new to Ajax and I\'m trying to disable a checkbox if certain items are selected in a dropdown. I need to pass in the mlaId to the GetMlaDeliveryType(int Id) method in
Set data
in the Ajax call so that its key matches the parameter on the controller (that is, Id
):
data: { Id: mlaId },
Note also that it's a better practice to use @Url.Action(actionName, controllerName)
to get an Action URL:
url: '@Url.Action("GetMlaDeliveryType", "Recipients")'
$.ajax({
type: 'GET',
url: '/Recipients/GetMlaDeliveryType',
data: { id: mlaId },
cache: false,
success: function(result) {
}
});
then fix your controller action so that it returns an ActionResult, not a string. JSON would be appropriate in your case:
public string GetMlaDeliveryType(int Id)
{
var recipientOrchestrator = new RecipientsOrchestrator();
// Returns string "Electronic" or "Mail"
return Json(
recipientOrchestrator.GetMlaDeliveryTypeById(Id),
JsonRequestBehavior.AllowGet
);
}
Now your success callback will directly be passed a javascript instance of your model. You don't need to specify any dataType
parameters:
success: function(result) {
// TODO: use the result here to do whatever you need to do
}