Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
How to POST javascript object, javascript object list and file list to asp.net core controller

I want to pass all data to controller after clicking button.

 var data = new FormData();

Here is my javascript object

var info = {};
info.CorporateName = $("input[name*='CorporateName']").val();
info.CorporatePhone1 = $("input[name*='CorporatePhone1']").val();
info.CorporatePhone2 = $("input[name*='CorporatePhone2']").val();
info.CorporateEmail = $("input[name*='CorporateEmail']").val();

I can reach these datas from the controller with code below

$.each(info, function (key, input) {
            data.append(key, input);
        });

Here is my file list. I can reach these files from the controller with code below

var allfiles = $(".required-files");

for (var i = 0; i < allfiles.length; i++)
{
  var files = allfiles[i].files;
  data.append('files', files[0]);
}

Here is my packages cookie. I can't reach package list from the controller

var packageList = $.parseJSON($.cookie("packageList"));
 data.append('packageList',JSON.stringify(packageList));

I can post files, my info object but I can't post cookie list

Here is my controller

[HttpPost]
public async Task<IActionResult> create(IEnumerable<IFormFile> files, List<PostServiceDto> packageList, ModelDto Model)
        {
          // files = not null, Model not null, but packageList = null

            var model = Request.Form;

            return View();
        }

My package model

 public class PostServiceDto
    {
        public string Type { get; set; }
        public string Name { get; set; }
        public string Price { get; set; }
        public string Id { get; set; }
        public string Count { get; set; }
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I couln't take the list it with parameter but I solved it getting json object form request body.

the value returned from request ={\"Type\":\"3\",\"Name\":\"dfsfsdfs\",\"Price\":\"44,00\",\"Id\":\"2\",\"Count\":1},{\"Type\":\"3\",\"Name\":\"Yeni Paket\",\"Price\":\"49,90\",\"Id\":\"1\",\"Count\":1} Solution

foreach (var item in Request.Form.Keys)
            {
                if(item == "packageList")
                {
                    clearObject(Request.Form[item]);


                }
            }



public List<PostServiceDto> clearObject(string value)
        {
            List<PostServiceDto> postServices = new List<PostServiceDto>();
            List<PostServiceDto> myDeserializedObjList =
                (List<PostServiceDto>)Newtonsoft.Json.JsonConvert.DeserializeObject(value, typeof(List<PostServiceDto>));
            return myDeserializedObjList;

        }

code source : [https://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C]

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!