Sending ajax post to mcv action

Leo Del Ciello 1 Reputation point
2021-02-01T11:41:25.737+00:00

Hello

I am developing an Asp.net Core Mvc application in which I need to send , as usual, some data from an ajax call to an MVC action.
The ajax code is as follows :

**function ShowHideAttivita(model, idriga) {
    var dati =model;
    var idcommessa = $('#IdselectCommessa' + idriga + ' option:selected').val();
    $.ajax({

        url: 'cambiaCommessa',
        type: 'POST',
        dataType: "html",
        contentType: "application/json",
        data: dati,
        success: function (result) {
            $('#IdrigaSezioneCommessa' + idriga).replaceWith(result);
        }, error: function (error) {
            var x = error.responseText;
            VisualizzaMessaggio('errore', 'E', '');
        }
    });
}**

The corresponding MVC Controller Action which is listening is as follows ;

**[HttpPost]
        public async Task<ActionResult> cambiaCommessa( RapGiornalieroExtended dati)
        {
            try
            {
                DettaglioPresenze DP = new DettaglioPresenze();
                return PartialView("~/Views/Home/_rigaCommessa.cshtml", DP);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }**

It seems I can call the action correctly, but the content in the data sent is null (the fields of the "dati" parameters).No matter how I change the code in the sending Ajax, when I break into the Action , the best result I can get is getting a "dati" parameter (which is a complex type itself) with all its fields null, in spite of the fact that, in the sending ajax, i verified the content is not null.
Most inquietingly, another application from which this same bunch of code is taken works perfectly , and there are no differences in the Ajax, in the Controller, in the Action, and in the data content itself, between the two situations.

Any suggestions ?
Thanks

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,162 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
{count} votes

1 answer

Sort by: Most helpful
  1. murtaza gandhi 81 Reputation points
    2021-02-05T10:14:43.957+00:00

    Hi,

    Are you using submit button , type = submit or type = button.

    type = button

    may solve it

    Regards.

    0 comments No comments