guy49987
guy49987

Reputation: 104

angularjs $http post object always null

I'm trying to send an object to the backend with an $http post, but one of the parameters is always null. I'm formatting the dto in the same way when saving a new object and that works fine, but when I try to call the update function it's not working. What am I missing?

This is my controller code:

vm.postUpdateITSM = function (itsm) {
        $http({
            method: "POST",
            url: "api/sources/" + itsm.Id,
            data: {
                id: itsm.Id,
                dto: {
                    ConnectorType: itsm.Type,
                    SourceName: itsm.ServerName,
                    DisplayName: itsm.DisplayName,
                    Credentials: JSON.stringify(itsm.UserName,
                                                itsm.Password),
                    Url: itsm.URL,
                    Settings: JSON.stringify(itsm.ResolveAlerts ? itsm.ResolveAlerts : false,
                                             itsm.AcknowledgeAlerts ? itsm.AcknowledgeAlerts : false,
                                             itsm.SyncInterval,
                                             itsm.IncidentInterval,
                                             itsm.Status ? itsm.Status : "")
                }
            }
        });
    }

And on the back end: The dto is always null when called.

public async Task<IHttpActionResult> Update(int id, [FromBody] SourceDto dto)
    {
        var source = Mapper.Map<Source>(dto);
        source.SourceID = id;
        source.ServerCount = "";
        var res = await SystemActors.SourceManager.Ask(new UpdateSource(source));

        var failure = res as Status.Failure;
        if (failure != null)
        {
            return InternalServerError();
        }
        var success = ((SqlResult<object>) res).Success;
        if (!success)
        {
            return Content(HttpStatusCode.BadRequest, "Failed to update source.");
        }

        return Ok(new ResponsePackage {Success = true});
    }

And this is the SourceDto class:

public class SourceDto
{
    public string ConnectorType { get; set; }
    public string SourceName { get; set; }
    public string DisplayName { get; set; }
    public string Credentials { get; set; }
    public string Url { get; set; }
    public string Settings { get; set; }
}

Upvotes: 0

Views: 1263

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Your frontend data is formatted a bit wrong - the data parameter should just be the one object your ASP.NET controller is expecting in the [FromBody], your SourceDto model - and the id should be a query string:

    method: "POST",
        url: "api/sources/" + itsm.Id,
        data: {
                ConnectorType: itsm.Type,
                SourceName: itsm.ServerName,
                DisplayName: itsm.DisplayName,
                Credentials: JSON.stringify(itsm.UserName,
                                            itsm.Password),
                Url: itsm.URL,
                Settings: JSON.stringify(itsm.ResolveAlerts ? itsm.ResolveAlerts : false,
                                         itsm.AcknowledgeAlerts ? itsm.AcknowledgeAlerts : false,
                                         itsm.SyncInterval,
                                         itsm.IncidentInterval,
                                         itsm.Status ? itsm.Status : "")
        }
    });

ASP.NET will apply the request body to the expected model - if it doesn't match, you'll get null

Upvotes: 1

Related Questions