简体   繁体   中英

Accept a list of objects from form data in ASP.NET Core

I am currently struggling to accept a list of objects from FormData in ASP.NET Core.

The project looks like this:

  1. I have a class called Stavka (English: Item).
public class Stavka
{
    public string naziv { get; set; }
    public double cenaPoJedinici { get; set; }
    public string jedinicaMere { get; set; }
    public int kolicina { get; set; }

    public Stavka(string naziv, double cenaPoJedinici, string jedinicaMere, int kolicina)
    {
        this.naziv = naziv;
        this.cenaPoJedinici = cenaPoJedinici;
        this.jedinicaMere = jedinicaMere;
        this.kolicina = kolicina;
    }

    public Stavka()
    {
    }
}
  1. I have a class called Faktura (English: Bill) which has a variable called Stavke (English: Items) that is a list containing the Stavka objects.
public class Faktura
{

    public int Id { get; set; }
    public string pibStart { get; set; }
    public string pibEnd { get; set; }
    public DateTime datumGen { get; set; }
    public DateTime datumRok { get; set; }
    public List<Stavka> stavke { get; set;}
    public double cena { get; set; }
    public string tip { get; set; }

    public Faktura(int id, string pibStart, string pibEnd, DateTime datumGen, DateTime datumRok, List<Stavka> stavke, string tip)
    {
        Id = id;
        this.pibStart = pibStart;
        this.pibEnd = pibEnd;
        this.datumGen = datumGen;
        this.datumRok = datumRok;
        this.stavke = stavke;
        this.tip = tip;

        double sumCena = 0;

        foreach(Stavka s in stavke)
        {
            sumCena += s.kolicina * s.cenaPoJedinici;
        }
        this.cena = sumCena;
    }

    public Faktura()
    {
    }
  1. I want to create a new Faktura object and add it to a list within my Controller. I tried to do this with the following code:
[HttpPost("dodajFakturu")]
public IActionResult dodajFakturu([FromForm]string pibStart, [FromForm]string pibEnd,[FromForm]DateTime datumStart, [FromForm]DateTime datumEnd,[FromForm]List<Stavka> stavkeLis, [FromForm]string tip)
{
    
    int id = lst.OrderByDescending(p => p.Id).First().Id + 1;
    Faktura f = new Faktura(id, pibStart,pibEnd, datumStart,datumEnd,stavkeLis,tip);

    lst.Add(f);
    return Ok(SveFakture());
}

And yet, when i post the request (in Swagger/Postman), the variable stavkeLis (which accepts the JSON array) is always empty:

Postman的GIF演示

This is certainly because i fundamentally misunderstood the way in which NET Core accepts these variables.

Is there some other way to send a list of objects through form data?

this way you have is currect, but if its not maybe because simple code problem but way that you right the code can be better or you can say develop your code as Below:

// StavkaBody => I Mean All Body In One Json
public async Task<IActionResult> MethodName([FromForm] string 
StavkaBody)
{
  YourObjectType object = new YourObjectType();
 // this will be Populate All Json To Single object And
 // You dont Need To Add some Constructors For Done this
 JsonConvert.PopulateObject(StavkaBody, objec);

 // Example Usage
 Console.WriteLine(object.Name);
}

in Here I`ve Used The Newtonsoft.Json For this And Its Make Your Model So Much Simpler.

I Hope Its Helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM