3

I have a model class. That class contains multiple properties , list of custom class and list of formfile data. I need to create post request for this model using Asp.net Core.

Model Class

public class TenoModel
    {
        [Key]
        public int Id { get; set; }

        public DateTime Date { get; set; }

        public float Salary { get; set; }

        [Required]
        public string User { get; set; }
        public List<IformFile> files {get;set;}
        public List<Employee> employees{get;set;}
}

Custom class for employee

public class Employee
{
 public int Id {get;set;}
 public string Name {get;set;}
 public double Salary {get;set;}
}

HttpPost request:

[HttpPost("CreateTeno")]
[Consumes('multipart/form-data']
public async Task<IActionResult> CreateTeno([FormFile] TenoModel model)
{
..... save the model data to teno database
}

When I execute the HttpPost request on swagger Formfile data and some property data come to incoming request on CreateTeno method. I fill the Employee object also in swagger. but that data not coming in my CreateTeno method. List of Employee data count zero. How to pass model class with multiple properties, List of IformFile and multiple list customclass in HTTP post request. Give a sample example.

1 Answer 1

1

You'll have to change the signature of your controller method like this:

public async Task<IActionResult> CreateTeno([FromForm] TenoModel model)

Tested with Postman like this

enter image description here

Please note that you have a syntax error here: [Consumes('multipart/form-data'], should be [Consumes("multipart/form-data")]

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

2 Comments

Thanks you. It's working fine. But every time we put the input manually, Like employee[0].Name, employee[1].Name ...etc., If My object have multiple properties I take lot of time to put the input in postman. Any possible we put the input like object in postman.
@user16490152: Good to hear that things are working now. I don't believe there are other ways of doing this in Postman. Even in C# you will have to make a custom converter like this one: gunnarpeipman.com/serialize-url-encoded-form-data BR

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.