0

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; }
}
1
  • Which parameter is null? Check the data in your postUpdateITSM function or in the network request - make sure you are populating the values you are expecting Commented May 4, 2017 at 22:00

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

This works, thank you. But I'm not sure I understand how the ID is passed if it's not inside the data parameter, since it's a parameter on the function.
@sdg91 - It's passed in via the URL url: "api/sources/" + itsm.Id,
The data parameter the OP posted is one object
@cnorthfield - Edited to clarify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.