0

I've an async action method that return a partial view. this action method called from a function with ajax.

my problem : when this method invoked every thing looks good except returned partial view. I got error 500.

this is my action method Code :

[HttpPost]
public async Task<ActionResult> ChangeStateGroup(int[] lstCustomerServiceId, int newState)
{
    int _counter = 0;
    try
    {           
            var _isOk = await CommonFunctions.ChangeState(_customerServiceId, _stateId, string.Empty);

            if (_isOk)
            {
                //------do somthing
            }
        }
        TempData[MyAlerts.SUCCESS] = string.Format("succeed Operation {0}", _counter);
    }
    catch (Exception ex)
    {
        TempData[MyAlerts.ERROR] = string.Format("Error: {0}", ex.Message);
    }
    return PartialView("_AlertsPartial");
}

and this is my jquery Code:

function postCustomerChangeStateGroup(lstCustomersId) {
    
    var arrCustomersId = lstCustomersId.split(',');
    $.ajax({
        type: "POST",
        url: "../Customer/ChangeStateGroup",
        data: {
            lstCustomerServiceId: arrCustomersId,
            newState: $("#fk_state").val()
        },
        success: function (data) {
            if (data.indexOf("error") !== -1) {
                $("#inlineAlert_Wrapper").append(data);
            }
            else {
                getCustomerReport();
                $('#modal-container').modal('toggle');
                $("#alert_Wrapper").append(data);
            }
        },
        failure: function (errMsg) {
            alert("An Error Accoured: " + errMsg);
        }
    });
}
Cawboy
  • 123
  • 2
  • 8

2 Answers2

1

Partial views cannot be asynchronous in ASP.NET pre-Core. You can have asynchronous partial views in ASP.NET Core.

So, your options are:

  1. Update to ASP.NET Core.
  2. Remove the asynchronous code and make it synchronous instead.
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

Error 500 means the error inside of the action.It maybe because ot the empty input parameters. Check them in debugger and if they are empty, try to use application/json content type,sometimes it works better

 $.ajax({
        type: "POST",
         contentType: "application/json; charset=utf-8",
        url: "/Customer/ChangeStateGroup",
        data: JSON.stringify( {
            lstCustomerServiceId: arrCustomersId,
            newState: $("#fk_state").val()
        }),
        success: function (data) {
        ....

create viewModel

public class ViewModel
{
 public int[] lstCustomerServiceId {get; set;}
public  int newState {get; set;}
}

and fix the action

public async Task<ActionResult> ChangeStateGroup([FromBody] ViewModel model)
Serge
  • 40,935
  • 4
  • 18
  • 45