3

I have a post method with below signature,

[HttpPost] public ActionResult SavePriorAuthorization(MainPriorAuthorization priorAuthorization, IFormFile file)

Now I want to pass the object along with the file from a postman. I have tried the following option which doesn't work.

enter image description here

This gives an error, System.ArgumentNullException: Value cannot be null.Parameter name: header

Header Type : multipart/form-data

Any help would be appreciated.

2 Answers 2

1

What I usually do is create a ViewModel like this one:

public class MainPriorAuthorizationViewModel
{   
    public IFormFile File { get; set; }
    public string TestName { get; set; }
}

Then create an action with [FromForm] attribute so that it knows from where it needs to mapped:

[HttpPost]
public void Post([FromForm]MainPriorAuthorizationViewModel priorAuthorization)
{
   //do logic
}

Then in my postman its look like this:

enter image description here

Hope this helps

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

Comments

1

Try to change the settings of the key in the MainPriorAuthorization model, you could directly set properties name of the model as the key in Postman.

The following is the example code that I tested and worked well:

Guest Model

 public class Guest
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Add the [FromForm] attribute to the parameter in action

[HttpPost]
    public void SaveGuest([FromForm]Guest guest,IFormFile file)
    {  }

The screenshot of the Postman enter image description here

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.