Coder Social home page Coder Social logo

System.InvalidCastException: Unable to cast object of type 'report_engine.Models.CustomerPlanModel' to type 'System.Collections.IList' about aspnetcorecsvimportexport HOT 7 CLOSED

damienbod avatar damienbod commented on July 3, 2024
System.InvalidCastException: Unable to cast object of type 'report_engine.Models.CustomerPlanModel' to type 'System.Collections.IList'

from aspnetcorecsvimportexport.

Comments (7)

damienbod avatar damienbod commented on July 3, 2024

Hi @rcalderoni
You need to use an IList type to import the data. In your Controller, your import method should look something like this:

[HttpPost]
[Route("import")]
public IActionResult Import([FromBody]List<CustomerPlanModel> value)
{
  if (!ModelState.IsValid)
  {
                return BadRequest(ModelState);
  }
  else
  {
                List<CustomerPlanModel> data = value;
                return Ok();
  }
}

Can you check this or post your Controller import method and I can check it?

If this is ok, can you post me your Controller code for the import and also the project.json

Greetings Damien

from aspnetcorecsvimportexport.

rcalderoni avatar rcalderoni commented on July 3, 2024

yep, that's what I've got as well, makes me wonder if it's a compatibility issue since I am using the latest .NET Core SDK, I'll report back if I find any fixes in that case

[HttpPost]
        [Route("import")]
        public IActionResult Import([FromBody]List<CustomerPlanModel> value)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            else
            {
                List<CustomerPlanModel> data = value;
                return Ok();
            }
        }
public void ConfigureServices(IServiceCollection services)
        {
            var csvFormatterOptions = new CsvFormatterOptions();

            // Add framework services.
            services.AddMvc(options =>
            {
                options.InputFormatters.Add(new CsvInputFormatter(csvFormatterOptions));
                options.OutputFormatters.Add(new CsvOutputFormatter(csvFormatterOptions));
                options.FormatterMappings.SetMediaTypeMappingForFormat("csv", MediaTypeHeaderValue.Parse("text/csv"));
            });

            services.AddTransient<IReportFilterService, ReportFilterService>();
            services.AddTransient<IReportFilterRepository, ReportFilterRepository>();
        }

from aspnetcorecsvimportexport.

damienbod avatar damienbod commented on July 3, 2024

If your using the an older version, then it's a compatibility issue, you need to change the Activator. The code works with the actual version of ASP.NET Core.

Greetings Damien

from aspnetcorecsvimportexport.

damienbod avatar damienbod commented on July 3, 2024

can I close this issue?

from aspnetcorecsvimportexport.

rcalderoni avatar rcalderoni commented on July 3, 2024

from aspnetcorecsvimportexport.

damienbod avatar damienbod commented on July 3, 2024

OK, I'll check it again with Version ASP.NET Core 1.1 and if it works, I'll close the issue.

Thanks for the replay

Greetings Damien

from aspnetcorecsvimportexport.

damienbod avatar damienbod commented on July 3, 2024

@rcalderoni

I checked this and it works without problems.

https://github.com/damienbod/AspNetCoreCsvImportExport/tree/delimiterTestComma

This was the update in ASP.NET Core version which caused this mismatch. Sorry this solution did not work for you.

Greetings Damien

from aspnetcorecsvimportexport.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.