3
votes

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

2 Answers

1
votes

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

1
votes

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