2

I need to pass two objects in the IActionResult method in Asp.net core. I try the following method but one object pass with the null values. what are the changes should I follow?

public IActionResult createService([FromBody] ArmSmServiceCreatorEntity CreateServiceDetail, [FromBody] ArmSmServiceAndSubServiceEntity createSSSDetails)
{
    try
    {
        this.manager.BeginTransaction();

        if (CreateServiceDetail == null)
        {
            return BadRequest("Service is null.");
        }

        this.manager.ArmSmServiceCreatorRepository.Add(CreateServiceDetail);

        if (createSSSDetails == null)
        {
            return BadRequest("Service is null.");
        }

        this.manager.ArmSmServiceAndSubServiceRepository.Add(createSSSDetails);
        this.manager.Commit();

        return Ok("File was processed.");
    }
    catch (Exception ex)
    {
        this.manager.Rollback();
        throw ex;
    }
}
1
  • Add the json which you are passing from client-side. Commented Feb 25, 2021 at 10:14

1 Answer 1

1

You can only have one FromBody parameter.

The documentation says:

Don't apply [FromBody] to more than one parameter per action method. Once the request stream is read by an input formatter, it's no longer available to be read again for binding other [FromBody] parameters.

Use a single object that wraps the two parameters:

public class ArmSmServiceRequest {
    ArmSmServiceCreatorEntity CreateServiceDetail { get; set; }
    ArmSmServiceAndSubServiceEntity CreateSSSDetails { get; set; }
}

Your method will look like:

public IActionResult createService([FromBody] ArmSmServiceRequest)

On the client side you also need to wrap two objects into one.

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

Comments

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.