jQuery Ajax call to controller

前端 未结 2 1958
一个人的身影
一个人的身影 2020-12-07 02:37

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

相关标签:
2条回答
  • 2020-12-07 03:04

    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")'
    
    0 讨论(0)
  • 2020-12-07 03:16
    $.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
    }
    
    0 讨论(0)
提交回复
热议问题